fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef signed long long ll;
  5. typedef pair<ll, ll> pll;
  6. const ll mod = 1e9 + 7;
  7. map<pll, int> mp;
  8. pll Arr[20];
  9. int n;
  10.  
  11. void solve(ll a, ll b, int i){
  12. if(i == n){
  13. ll mn = min(a, b), mx = max(a, b);
  14. mp[pll(mn, mx)]++;
  15. }else{
  16. ll sz = Arr[i].second, p = Arr[i].first, _a, _b;
  17. _a = ((a % mod) * ((ll)pow(p, sz)) % mod) % mod;
  18.  
  19. for(ll j = 0; j <= sz; j++){
  20. _b = ((b % mod) * ((ll)pow(p, j)) % mod) % mod;
  21. solve(_a, _b, i + 1);
  22. }
  23.  
  24. _b = ((b % mod) * ((ll)pow(p, sz)) % mod) % mod;
  25. for(ll j = 0; j <= sz; j++){
  26. _a = ((a % mod) * ((ll)pow(p, j)) % mod) % mod;
  27.  
  28. solve(_a, _b, i + 1);
  29. }
  30. }
  31. }
  32.  
  33. void solve(int i){
  34. ll sum = 0;
  35. solve(1LL, 1LL, 0);
  36. map<pll, int>:: iterator it;
  37. for(it = mp.begin(); it != mp.end(); it++){
  38. pll pr = it->first;
  39. sum = (sum + (pr.first + pr.second) % mod) % mod;
  40. }
  41. printf("Case %d: %lld\n", i, sum);
  42. }
  43.  
  44. int main(){
  45. int t;
  46. ll p, cnt;
  47. scanf("%d", &t);
  48. for(int i = 1; i <= t; i++){
  49. scanf("%d", &n);
  50. for(int j = 0; j < n; j++){
  51. scanf("%lld %lld", &p, &cnt);
  52. Arr[j] = pll(p, cnt);
  53. }
  54.  
  55. mp.clear();
  56. solve(i);
  57. }
  58. return 0;
  59. }
Time limit exceeded #stdin #stdout 5s 17088KB
stdin
1
15
2 50
3 50
5 50
7 50
11 50
13 50
17 50
19 50
23 50
29 50
31 50
37 50
41 50
43 50
47 50
stdout
Standard output is empty