Advertisement
Guest User

Untitled

a guest
Feb 4th, 2021
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int dp[12][120][120][2];
  4. int solve(string &s, int k, int pos=0, int sum=0, int tight=0, int rem=0)
  5. {
  6. if(pos==s.size())
  7. {
  8. if(sum%k==0 && rem%k==0)
  9. return 1;
  10. return 0;
  11. }
  12. if(dp[pos][sum][rem][tight]!=-1)
  13. return dp[pos][sum][rem][tight];
  14. int ans=0;
  15. if(tight==1)
  16. {
  17. for(int i=0;i<=s[pos]-'0';i++)
  18. {
  19. if(i==s[pos]-'0')
  20. ans=ans+solve(s,k,pos+1,(sum+i)%k,1,(rem*10+i)%k);
  21. else
  22. ans=ans+solve(s,k,pos+1,(sum+i)%k,0,(rem*10+i)%k);
  23. }
  24. dp[pos][sum][rem][tight]=ans;
  25. }
  26. else if(tight==0)
  27. {
  28. for(int i=0;i<=9;i++)
  29. {
  30. ans=ans+solve(s,k,pos+1,(sum+i)%k,0,(rem*10+i)%k);
  31. }
  32. dp[pos][sum][rem][tight]=ans;
  33. }
  34. return ans;
  35. }
  36. void sol()
  37. {
  38. int a,b,k;
  39. cin>>a>>b>>k;
  40. memset(dp,-1,sizeof(dp));
  41. string s1=to_string(a-1);
  42. int a1=solve(s1,k);
  43. memset(dp,-1,sizeof(dp));
  44. string s2=to_string(b);
  45. int b1=solve(s2,k);
  46. cout<<b1-a1;
  47. }
  48. int main()
  49. {
  50. #ifndef ONLINE_JUDGE
  51. freopen("input.txt","r",stdin);
  52. freopen("output.txt","w",stdout);
  53. #endif
  54. int t;
  55. cin>>t;
  56. while(t--)
  57. {
  58. sol();
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement