Nusrat_Ullah

Product sum

Apr 28th, 2021 (edited)
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll M = 1000000007;
  5. ll st[20][20];
  6. ll calculate(ll z)
  7. {
  8.     int g,h,d,nt=0;
  9.     ll re=0,x=z;
  10.     vector<int>dg;
  11.     while(x){
  12.         nt++;
  13.         dg.push_back(x%10);
  14.         x/=10;
  15.     }
  16.     for(g=nt;g>0;g--){
  17.         d=dg[g-1];
  18.         if(g==nt||g==1)x=st[g-1][d];
  19.         else x=st[g-1][d]-st[g-1][0];
  20.         for(h=g;h<nt;h++){
  21.             x*=dg[h];
  22.             x%=M;
  23.         }
  24.         re+=x;
  25.         re%=M;
  26.     }
  27.     return re;
  28. }
  29. int main()
  30. {
  31.     int g,h,t,s=1;
  32.     ll q,w,re;
  33.     st[0][0]=0;
  34.     for(g=1;g<=9;g++)
  35.         st[0][g]=st[0][g-1]+g;
  36.     st[0][10]=45;
  37.     for(g=1;g<=18;g++){
  38.         st[g][0]=st[g-1][10];
  39.         for(h=1;h<=10;h++){
  40.             st[g][h]=((st[g][0]*(h-1))%M+st[g][h-1]-(st[g-1][0]*(h-1))%M)%M;
  41.             if(st[g][h]<0)st[g][h]+=M;
  42.         }
  43.     }
  44.     scanf("%d",&t);
  45.     while(t--){
  46.         scanf("%lld %lld",&q,&w);
  47.         re=calculate(w)-calculate(q-1);
  48.         if(re<0)re+=M;
  49.         printf("Case %d: %lld\n",s++,re);
  50.     }
  51.     return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment