chang2394

AMRITA5

Oct 11th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. // uzumaki naruto
  2. #include <bits/stdc++.h>
  3. #define each(v,c)  for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
  4. #define pb         push_back
  5. #define mp         make_pair
  6. #define sz(a)      ((int)(a.size()))
  7. #define all(a)     (a).begin(), (a).end()
  8. #define fi         first
  9. #define se         second
  10. #define TRACE
  11. using namespace std;
  12.  
  13. #ifdef TRACE
  14. #define debug(a,n)    cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
  15. #define dbg(args...)  {debug1,args; cerr<<endl;}
  16. #define pause()       cin.get();cin.get();
  17.  
  18. #else
  19.  
  20. #define debug(a,n)
  21. #define dbg(args...)
  22. #define pause()
  23.  
  24. #endif
  25.  
  26. struct debugger {
  27.     template<typename T> debugger& operator , (const T& v) {
  28.         cerr<<v<<" "; return *this;
  29.     }
  30. } debug1;
  31.  
  32. template <typename T1, typename T2>
  33. inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
  34.     return os << "(" << p.first << ", " << p.second << ")";
  35. }
  36.  
  37. template<typename T>
  38. inline ostream &operator << (ostream & os,const vector<T>& v) {
  39.     bool first = true; os << "[";
  40.     for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
  41.         if(!first) os << ", ";
  42.         os << *ii; first = false;
  43.     }
  44.     return os << "]";
  45. }
  46.  
  47. typedef long long LL;
  48. typedef pair<LL,LL> pii;
  49. typedef vector<int> vi;
  50.  
  51. const LL mod = 1000000007;
  52.  
  53. pii f(LL n, LL dd){
  54.     if (n == 1){
  55.         LL ans = 0;
  56.         for(int i = 0; i < dd; ++i)
  57.             ans = (ans + i)%mod;
  58.         return mp(ans,dd);
  59.     }
  60.  
  61.     pii res = f(n/2,dd);
  62.     LL sum = res.fi, cnt = res.se;
  63.  
  64.     pii nine = f(n/2,9LL);
  65.     LL ans = 0;
  66.     ans = (ans + cnt*(nine.fi))%mod;
  67.     ans = (ans + (nine.se*sum)%mod )%mod;
  68.  
  69.     ans = (ans + cnt*(9LL*(n/2LL))%mod )%mod;
  70.     ans = (ans + sum)%mod;
  71.  
  72.     ans = (ans + cnt*(dd*(n/2LL))%mod )%mod;
  73.     ans = (ans + sum)%mod;
  74.  
  75.     LL new_cnt = 0;
  76.     new_cnt = ((cnt * (nine.se+1))%mod + new_cnt)%mod;
  77.     new_cnt = (new_cnt + cnt)%mod;
  78.  
  79.     LL a1 = 0LL, a2 = 0LL;
  80.     if (n & 1){
  81.         pii nn = f(n-1LL,9LL);
  82.         for(LL i = 0; i < dd; ++i){
  83.             a1 = (a1 + (i*(nn.se+1LL))%mod ) %mod;
  84.             a1 = (a1 + nn.fi)%mod;
  85.             a1 = (a1 + (9LL*(n-1LL))%mod )%mod;
  86.             a2 = (a2 + (nn.se+1LL))%mod;
  87.         }
  88.  
  89.         a1 = (a1 + ans)%mod;
  90.         a1 = (a1 + (dd*cnt)%mod )%mod;
  91.         a2 = (a2 + new_cnt)%mod;
  92.  
  93.         ans = a1;
  94.         new_cnt = a2;
  95.     }
  96.     return mp(ans,new_cnt);
  97. }
  98.  
  99. #define M 1000000007
  100. long long dig[10004];
  101. long long len[10004];
  102. bool vis[10004][2];
  103. long long dp[10004][2];
  104. int m;
  105.  
  106. long long cal(int idx, int tight)
  107. {
  108.     if ( idx == m ) return 0LL;
  109.     if ( vis[idx][tight] ) return dp[idx][tight];
  110.     vis[idx][tight] = true;
  111.     long long ans;
  112.     if ( tight == 0 ) {
  113.         pii tmp = f(len[idx],9LL);
  114.         ans = (tmp.fi + ((tmp.se+1LL)*cal(idx+1,0))%M)%M;
  115.         ans = (ans + (9LL*len[idx])%M)%M;
  116.     }
  117.     else {
  118.         pii tmp = f(len[idx],dig[idx]);
  119.         ans = (tmp.fi + (tmp.se * cal(idx+1,0))%M)%M;
  120.         ans = (ans + (len[idx]*dig[idx])%M)%M;
  121.         ans = (ans + cal(idx+1,1))%M;
  122.     }
  123.     dp[idx][tight] = ans;
  124.     return ans;
  125. }
  126. int main()
  127. {
  128.     int t;
  129.     cin >> t;
  130.     while ( t-- ) {
  131.         memset(vis,false,sizeof(vis));
  132.         cin >> m;
  133.         for ( int i = 0; i < m; i++ ){
  134.             cin >> len[i] >> dig[i];
  135.             len[i] = len[i]%M;
  136.         }
  137.  
  138.         long long ans = cal(0,1);
  139.         cout << ans << endl;
  140.     }
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment