Guest User

Untitled

a guest
Feb 5th, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. // #define mod 1000000007
  4. int mod=1e9+7;
  5. int m,d;
  6. int dp[2012][2][2012];//pos,tight,remainder
  7. int solve(string &s,int pos=0,int tight=1,int rem=0)
  8. {
  9. if(pos==s.size())
  10. {
  11. if(rem==0)
  12. return 1;
  13. return 0;
  14. }
  15. if(dp[pos][tight][rem]!=-1)
  16. return dp[pos][tight][rem];
  17. int ans=0;
  18. if(tight==1)
  19. {
  20. for(int i=0;i<=s[pos]-'0';i++)
  21. {
  22. if(pos%2==0 && i==d)
  23. continue;
  24. if(pos%2==1 && i!=d)
  25. continue;
  26. if(i==s[pos]-'0')
  27. ans=(ans+solve(s,pos+1,1,(rem*10+i)%m))%mod;
  28. else
  29. ans=(ans+solve(s,pos+1,0,(rem*10+i)%m))%mod;
  30. }
  31. dp[pos][tight][rem]=ans;
  32. }
  33. else if(tight==0)
  34. {
  35. for(int i=0;i<=9;i++)
  36. {
  37. if(pos%2==0 && i==d)
  38. continue;
  39. if(pos%2==1 && i!=d)
  40. continue;
  41. ans=(ans+solve(s,pos+1,0,(rem*10+i)%m))%mod;
  42. }
  43. dp[pos][tight][rem]=ans;
  44. }
  45. return ans;
  46. }
  47. void sol()
  48. {
  49. // int m,d;
  50. cin>>m>>d;
  51. int x,y;
  52. cin>>x>>y;
  53. x=x-1;
  54. string a=to_string(x);
  55. memset(dp,-1,sizeof(dp));
  56. int a_ans=solve(a);
  57. string b=to_string(y);
  58. memset(dp,-1,sizeof(dp));
  59. int b_ans=solve(b);
  60. cout<<b_ans-a_ans;
  61. }
  62. int main()
  63. {
  64. #ifndef ONLINE_JUDGE
  65. freopen("input.txt","r",stdin);
  66. freopen("output.txt","w",stdout);
  67. #endif
  68. int t;
  69. t=1;
  70. while(t--)
  71. {
  72. sol();
  73. cout<<endl;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment