Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uzumaki naruto
- #include <bits/stdc++.h>
- #define each(v,c) for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
- #define pb push_back
- #define mp make_pair
- #define sz(a) ((int)(a.size()))
- #define all(a) (a).begin(), (a).end()
- #define fi first
- #define se second
- #define TRACE
- using namespace std;
- #ifdef TRACE
- #define debug(a,n) cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
- #define dbg(args...) {debug1,args; cerr<<endl;}
- #define pause() cin.get();cin.get();
- #else
- #define debug(a,n)
- #define dbg(args...)
- #define pause()
- #endif
- struct debugger {
- template<typename T> debugger& operator , (const T& v) {
- cerr<<v<<" "; return *this;
- }
- } debug1;
- template <typename T1, typename T2>
- inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
- return os << "(" << p.first << ", " << p.second << ")";
- }
- template<typename T>
- inline ostream &operator << (ostream & os,const vector<T>& v) {
- bool first = true; os << "[";
- for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
- if(!first) os << ", ";
- os << *ii; first = false;
- }
- return os << "]";
- }
- typedef long long LL;
- typedef pair<LL,LL> pii;
- typedef vector<int> vi;
- const LL mod = 1000000007;
- pii f(LL n, LL dd){
- if (n == 1){
- LL ans = 0;
- for(int i = 0; i < dd; ++i)
- ans = (ans + i)%mod;
- return mp(ans,dd);
- }
- pii res = f(n/2,dd);
- LL sum = res.fi, cnt = res.se;
- pii nine = f(n/2,9LL);
- LL ans = 0;
- ans = (ans + cnt*(nine.fi))%mod;
- ans = (ans + (nine.se*sum)%mod )%mod;
- ans = (ans + cnt*(9LL*(n/2LL))%mod )%mod;
- ans = (ans + sum)%mod;
- ans = (ans + cnt*(dd*(n/2LL))%mod )%mod;
- ans = (ans + sum)%mod;
- LL new_cnt = 0;
- new_cnt = ((cnt * (nine.se+1))%mod + new_cnt)%mod;
- new_cnt = (new_cnt + cnt)%mod;
- LL a1 = 0LL, a2 = 0LL;
- if (n & 1){
- pii nn = f(n-1LL,9LL);
- for(LL i = 0; i < dd; ++i){
- a1 = (a1 + (i*(nn.se+1LL))%mod ) %mod;
- a1 = (a1 + nn.fi)%mod;
- a1 = (a1 + (9LL*(n-1LL))%mod )%mod;
- a2 = (a2 + (nn.se+1LL))%mod;
- }
- a1 = (a1 + ans)%mod;
- a1 = (a1 + (dd*cnt)%mod )%mod;
- a2 = (a2 + new_cnt)%mod;
- ans = a1;
- new_cnt = a2;
- }
- return mp(ans,new_cnt);
- }
- #define M 1000000007
- long long dig[10004];
- long long len[10004];
- bool vis[10004][2];
- long long dp[10004][2];
- int m;
- long long cal(int idx, int tight)
- {
- if ( idx == m ) return 0LL;
- if ( vis[idx][tight] ) return dp[idx][tight];
- vis[idx][tight] = true;
- long long ans;
- if ( tight == 0 ) {
- pii tmp = f(len[idx],9LL);
- ans = (tmp.fi + ((tmp.se+1LL)*cal(idx+1,0))%M)%M;
- ans = (ans + (9LL*len[idx])%M)%M;
- }
- else {
- pii tmp = f(len[idx],dig[idx]);
- ans = (tmp.fi + (tmp.se * cal(idx+1,0))%M)%M;
- ans = (ans + (len[idx]*dig[idx])%M)%M;
- ans = (ans + cal(idx+1,1))%M;
- }
- dp[idx][tight] = ans;
- return ans;
- }
- int main()
- {
- int t;
- cin >> t;
- while ( t-- ) {
- memset(vis,false,sizeof(vis));
- cin >> m;
- for ( int i = 0; i < m; i++ ){
- cin >> len[i] >> dig[i];
- len[i] = len[i]%M;
- }
- long long ans = cal(0,1);
- cout << ans << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment