Knobody

Untitled

Aug 10th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
  3. #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
  4. #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
  5. #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
  6. #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
  7. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  8. using namespace std;
  9.  
  10. typedef long long int big;
  11.  
  12. typedef  long double fig;
  13.  
  14.  
  15. big power(big x, big y, big p)
  16. {
  17.     big res = 1;      // Initialize result
  18.  
  19.     x = x % p;  // Update x if it is more than or  
  20.                 // equal to p
  21.  
  22.     while (y > 0)
  23.     {
  24.         // If y is odd, multiply x with result
  25.         if (y & 1)
  26.             res = (res*x) % p;
  27.  
  28.         // y must be even now
  29.         y = y>>1; // y = y/2
  30.         x = (x*x) % p;  
  31.     }
  32.     return res;
  33. }
  34.  
  35.  
  36. bool prime[100005];
  37.  
  38.  
  39. void SieveOfEratosthenes(big n)
  40. {
  41.     // Create a boolean array "prime[0..n]" and initialize
  42.     // all entries it as true. A value in prime[i] will
  43.     // finally be false if i is Not a prime, else true.
  44.      
  45.     //bool prime[n+1];
  46.     memset(prime, true, sizeof(prime));
  47.  
  48.     for (int p=2; p*p<=n; p++)
  49.     {
  50.         // If prime[p] is not changed, then it is a prime
  51.         if (prime[p] == true)
  52.         {
  53.             // Update all multiples of p greater than or  
  54.             // equal to the square of it
  55.             // numbers which are multiple of p and are
  56.             // less than p^2 are already been marked.  
  57.             for (int i=p*p; i<=n; i += p)
  58.                 prime[i] = false;
  59.         }
  60.     }
  61.  
  62.     // Print all prime numbers
  63.     /*for (int p=2; p<=n; p++)
  64.        if (prime[p])
  65.           cout << p << " ";*/
  66. }
  67.  
  68. big mod(vector<big> num, big a)
  69. {
  70.     // Initialize result
  71.     big res = 0;
  72.  
  73.     // One by one process all digits of 'num'
  74.     for (int i = 0; i < num.size(); i++)
  75.          res = (res*10 + num[i]) %a;
  76.  
  77.     return res;
  78. }
  79.  
  80. big mod(big a,big b){
  81.     return (a%b+b)%b;
  82. }
  83. big arr[]={0,1,3,6,10,15,21,28,36,45,45,46,48,51,55,60,66,73,81,90};
  84.  
  85. int main(){
  86.     //freopen("i.txt","r",stdin);
  87.     //freopen("o.txt","w",stdout);
  88.    
  89.    
  90.     big t=1;
  91.     cin>>t;
  92.     while(t--){
  93.         big n1,n2;
  94.  
  95.        
  96.         big p=1000000007;
  97.         big INV=700000005;
  98.         big FF=45;
  99.         /*for(big i=0;i<n;i++){
  100.             cin>>num[i];
  101.         }
  102.         cout<<mod(num,1000000007)<<endl;*/
  103.         string l;
  104.         string r;
  105.         cin>>n1>>l;
  106.         cin>>n2>>r;
  107.         big flag=1;
  108.         for(big i=r.size()-1;i>=0 && flag;i--){
  109.             if(r[i]=='9'){
  110.                 r[i]='0';
  111.             }
  112.             else{
  113.                 r[i]=(char)(r[i]+1);
  114.                 flag=0;
  115.             }
  116.         }
  117.         if(flag){
  118.             r="1"+r;
  119.             n2++;
  120.         }
  121.         string t;
  122.         for(big i=0;i<(n2-n1);i++){
  123.             t.push_back('0');
  124.         }
  125.         l=t+l;
  126.         //cout<<l<<endl;
  127.         //cout<<r<<endl;
  128.         big n=n2;
  129.         vector<big> num(n);
  130.         vector<big> cd(n);
  131.         for(big i=0;i<n;i++){
  132.             cd[i]=(big)(r[i]-l[i]);
  133.         }
  134.        
  135.         //dbr("cd",cd);
  136.         vector<big> tens(n);
  137.         tens[0]=1;
  138.         for(big i=1;i<n;i++){
  139.             tens[i]=mod((tens[i-1]*10),p);
  140.         }
  141.         //dbr("tens",tens);
  142.         vector<big> cdmt(n);
  143.         for(big i=0;i<n;i++){
  144.             cdmt[i]=mod(cd[i],10);
  145.         }
  146.         //dbr("cdmt",cdmt);
  147.        
  148.         vector<big> cdmp(n);
  149.         for(big i=0;i<n;i++){
  150.             cdmp[i]=mod(cd[i],p);
  151.         }
  152.         //dbr("cdmp",cdmp);
  153.         vector<big> diff(n);
  154.         diff[0]=cdmp[0];
  155.         for(big i=1;i<n;i++){
  156.             diff[i]=mod((mod(diff[i-1]*10,p)+cdmp[i]),p);
  157.         }
  158.         //dbr("diff",diff);
  159.         vector<big> key(n);
  160.        
  161.         for(big i=0;i<n;i++){
  162.             big temp=mod(mod((diff[i]+mod((-1)*cdmt[i],p)),p)*INV,p);
  163.         //  dbg("temp",temp);
  164.             key[i]=mod(temp*45,p);
  165.             big start=(big)(l[i]-'0');
  166.             temp=(start==0)?arr[cdmt[i]-1]:(arr[start+cdmt[i]-1]-arr[start-1]);
  167.             temp=mod(temp,p);
  168.             key[i]=mod((key[i]+temp),p);
  169.         }
  170.         //dbr("key",key);
  171.         vector<big> res(n);
  172.         for(big i=0;i<n;i++){
  173.             res[i]=mod((key[i]*tens[n-i-1]),p);
  174.         }
  175.         //dbr("res",res);
  176.         vector<big> sub1(n);
  177.         sub1[n-1]=(big)(l[n-1]-'0');
  178.         for(big i=n-2;i>=0;i--){
  179.             sub1[i]=mod((sub1[i+1]+mod(big(l[i]-'0')*tens[n-i-1],p)),p);
  180.         }
  181.         //dbr("sub1",sub1);
  182.        
  183.         vector<big> sub2(n);
  184.         sub2[n-1]=(big)(r[n-1]-'0');
  185.         for(big i=n-2;i>=0;i--){
  186.             sub2[i]=mod((sub2[i+1]+mod(big(r[i]-'0')*tens[n-i-1],p)),p);
  187.         }
  188.         //dbr("sub2",sub2);
  189.         for(big i=0;i<n-1;i++){
  190.             res[i]=mod((res[i]+mod(((-1)*(big)(l[i]-'0')*sub1[i+1]),p)),p);
  191.         }
  192.         //dbr("res",res);
  193.         for(big i=0;i<n-1;i++){
  194.             res[i]=mod((res[i]+mod(((1)*(big)(r[i]-'0')*sub2[i+1]),p)),p);
  195.         }
  196.         //dbr("res",res);
  197.         //cout<<"Normal result= "<<mod(res,p)<<endl;
  198.         for(big i=1;i<n;i++){
  199.             res[i]=mod((res[i]+mod((-1)*(key[i-1]*tens[n-i-1]),p)),p);
  200.         }
  201.         //dbr("res_after_normal",res);
  202.         for(big i=1;i<n-1;i++){
  203.             if(l[i]<l[i-1]){
  204.                 ;
  205.             }
  206.             else if(l[i]==l[i-1]){
  207.                 res[i]=mod((res[i]+mod(sub1[i+1]*(l[i]-'0'),p)),p);
  208.             }
  209.             else{
  210.                 res[i]=mod((res[i]+mod((l[i-1]-'0')*tens[n-i-1],p)),p);
  211.             }
  212.         }
  213.         if(l[n-1]>l[n-2]){
  214.             res[n-1]=mod((res[n-1]+mod(1*(l[n-2]-'0'),p)),p);
  215.         }
  216.         //dbr("res_after_first_correction",res);
  217.         for(big i=1;i<n-1;i++){
  218.             if(r[i]<r[i-1]){
  219.                 ;
  220.             }
  221.             else if(r[i]==r[i-1]){
  222.                 res[i]=mod((res[i]+mod(sub2[i+1]*(r[i]-'0')*(-1),p)),p);
  223.             }
  224.             else{
  225.                 res[i]=mod((res[i]+mod((r[i-1]-'0')*tens[n-i-1]*(-1),p)),p);
  226.             }
  227.         }
  228.         if(r[n-1]>r[n-2]){
  229.             res[n-1]=mod((res[n-1]+mod((-1)*(r[n-2]-'0'),p)),p);
  230.         }
  231.         //dbr("res_after_second_correction",res);
  232.         cout<<mod(res,p)<<endl;
  233.     }
  234.     return 0;
  235. }
Add Comment
Please, Sign In to add comment