Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dp[12][120][120][2];
- int solve(string &s, int k, int pos=0, int sum=0, int tight=0, int rem=0)
- {
- if(pos==s.size())
- {
- if(sum%k==0 && rem%k==0)
- return 1;
- return 0;
- }
- if(dp[pos][sum][rem][tight]!=-1)
- return dp[pos][sum][rem][tight];
- int ans=0;
- if(tight==1)
- {
- for(int i=0;i<=s[pos]-'0';i++)
- {
- if(i==s[pos]-'0')
- ans=ans+solve(s,k,pos+1,(sum+i)%k,1,(rem*10+i)%k);
- else
- ans=ans+solve(s,k,pos+1,(sum+i)%k,0,(rem*10+i)%k);
- }
- dp[pos][sum][rem][tight]=ans;
- }
- else if(tight==0)
- {
- for(int i=0;i<=9;i++)
- {
- ans=ans+solve(s,k,pos+1,(sum+i)%k,0,(rem*10+i)%k);
- }
- dp[pos][sum][rem][tight]=ans;
- }
- return ans;
- }
- void sol()
- {
- int a,b,k;
- cin>>a>>b>>k;
- memset(dp,-1,sizeof(dp));
- string s1=to_string(a-1);
- int a1=solve(s1,k);
- memset(dp,-1,sizeof(dp));
- string s2=to_string(b);
- int b1=solve(s2,k);
- cout<<b1-a1;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- #endif
- int t;
- cin>>t;
- while(t--)
- {
- sol();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement