Educational codeforces Round 8 D. Magic Numbers

Source: Internet
Author: User

Magic Numbers

Test instructions: Given a length of not more than 2000 of A, a, ask how many X (a<=x<=b) makes X's even digit is d, the odd digit is not D; result MoD 1e9+7;

Direct digital DP; Note that before judging whether f[pos][mod]! =-1, it is possible to determine whether it is a boundary, or the duplicate calculation will occur;

#include <bits/stdc++.h>using namespacestd;#defineINF 0x3f3f3f3f#defineMS1 (a) memset (A,-1,sizeof (a))Const intN =2005, MOD = 1e9+7;CharA[n],b[n];intf[n][n][2],n,m,d;intDfsChar* S,intPintMoDintOn =1){    if(p = = n)returnMoD = =0; int& ans =F[p][mod][on]; if(ans! =-1)returnans; Ans=0;  for(inti =0; I <= (On?s[p]-'0':9); i++){        if(p%2&& i! = d)Continue; if(p%2==0&& i = = d)Continue; (Ans+ = DFS (s,p+1, (mod*Ten+i)%m,on && i = = s[p]-'0')) %=MOD; }    returnans;}intCalcChar*s)    {MS1 (f); returnDFS (S,0,0);}intMain () {scanf ("%d%d",&m,&d); scanf ("%s%s", A, b); N=strlen (a);  for(inti = n1; I >=0; i--){        if(A[i] = ='0') A[i] ='9'; Else{a[i]--; Break;} } printf ("%d", (Calc (b)-calc (a) +mod)%MOD);}
View Code

Another is the addition of one dimension [2], which is used to store whether it is a boundary;

#include <bits/stdc++.h>using namespacestd;#defineINF 0x3f3f3f3f#defineMS1 (a) memset (A,-1,sizeof (a))Const intN =2005, MOD = 1e9+7;CharA[n],b[n];intf[n][n][2],n,m,d;intDfsChar* S,intPintMoDintOn =1){    if(p = = n)returnMoD = =0; int& ans =F[p][mod][on]; if(ans! =-1)returnans; Ans=0;  for(inti =0; I <= (On?s[p]-'0':9); i++){        if(p%2&& i! = d)Continue; if(p%2==0&& i = = d)Continue; (Ans+ = DFS (s,p+1, (mod*Ten+i)%m,on && i = = s[p]-'0')) %=MOD; }    returnans;}intCalcChar*s)    {MS1 (f); returnDFS (S,0,0);}intMain () {scanf ("%d%d",&m,&d); scanf ("%s%s", A, b); N=strlen (a);  for(inti = n1; I >=0; i--){        if(A[i] = ='0') A[i] ='9'; Else{a[i]--; Break;} } printf ("%d", (Calc (b)-calc (a) +mod)%MOD);}
View Code

Educational codeforces Round 8 D. Magic Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.