BJTU 1867. try a try, ac is OK

$\dfrac{f_i-f_j}{10^{n-j+1}}\equiv m\mod p$

$f_i\equiv 10^{n-j+1}m+f_j\mod p$

$1\not\equiv2\mod 5$

$10\equiv20\mod5$

#include<bits/stdc++.h>

using namespace std;

inline int rd(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
}
#define pc putchar
#define space() pc(' ')
#define nextline() pc('\n')
void pot(int x){if(!x)return;pot(x/10);pc('0'+x%10);}
void out(int x){if(!x)pc('0');if(x<0)pc('-'),x=-x;pot(x);}

typedef long long ll;

const int MAXN = 500005;

char S[MAXN];
int f[MAXN],b[MAXN],s[MAXN];
int n,p,m;

int sh[MAXN];

int mul(int x,int y){
ll xx=x,yy=y;
ll ret=(xx*yy)%p;
return (int)ret;
}

void spsolve(){
ll ans=0;
for(int i=1;i<=n;i++){
if(s[i]%p==m) ans+=i;
}
cout<<ans;
}

int main(){
scanf("%s",S+1);
p=rd();m=rd();
n=strlen(S+1);
for(int i=1;i<=n;i++) s[i]=S[i]-'0';
if(p==5||p==2) return spsolve(),0;
sh[0]=1;
for(int i=1;i<=n+1;i++) sh[i]=(sh[i-1]*10)%p;
ll ans=0;
for(int i=n;i>=1;i--)
f[i]=(f[i+1]+(sh[n-i]*s[i])%p)%p;
for(int i=1;i<=n+1;i++){
ans+=1ll*b[(f[i]+mul(m,sh[n-i+1]))%p];
b[f[i]%p]++;
}
cout<<ans<<endl;
return 0;
}

posted @ 2020-09-14 21:46  GhostCai  阅读(86)  评论(0编辑  收藏  举报