QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#496440#9139. Prefix Divisible by Suffixucup-team1231#AC ✓2267ms3692kbC++142.9kb2024-07-28 10:15:202024-07-28 10:15:20

Judging History

你现在查看的是最新测评结果

  • [2024-07-28 10:15:20]
  • 评测
  • 测评结果:AC
  • 用时:2267ms
  • 内存:3692kb
  • [2024-07-28 10:15:20]
  • 提交

answer

#pragma GCC optimize("Ofast","funroll-all-loops","ffast-math")
#include <bits/stdc++.h>
using namespace std;
#define SZ 1333333
#define fi first
#define se second
typedef long long ll;
typedef __int128 lll;
ll n,c;
ll um;
//(x*po+lo)%mo==0
ll po[30],mo[30],lo[30]; int sn;
int dc[30];
ll px[30],py[30];
ll ans=0;
ll euclid(ll a,ll b,ll&x,ll&y) {
    if(!b) return x=1,y=0,a;
    ll d=euclid(b,a%b,y,x);
    return y-=a/b*x,d;
}
void dfs(int cur,ll p,ll q,int coef=1) {
    q%=p; if(q<0) q+=p;
    if(q>um) return;
    if(um/p<=20) {
        for(ll w=q;w<=um;w+=p) {
            if(!w) continue;
            for(int u=cur;~u;--u)
                if((w%mo[u]*po[u]+lo[u])%mo[u]==0) goto bad;
            ans+=coef;
            // cerr<<"found "<<w<<"\n";
            bad:;
        }
        return;
    }
    if(cur<0) {
        // cerr<<"haha\n";
        ll rst=(um-q)/p+(q!=0);
        ans+=coef*rst;
        return;
    }
    dfs(cur-1,p,q,coef);
    if(!dc[cur]) {
        ll g=__gcd(po[cur],mo[cur]);
        if(lo[cur]%g) dc[cur]=-1;
        else {
            lo[cur]/=g, po[cur]/=g, mo[cur]/=g;
            ll x,y;
            euclid(po[cur],mo[cur],x,y);
            //x=inv(po[cur],mo[cur])
            ll RHS=lo[cur]*x%mo[cur];
            po[cur]=1; lo[cur]=RHS;
            dc[cur]=1;
            // cerr<<"dec:"<<mo[cur]<<"EZ\n";
        }
    }
    if(dc[cur]!=-1) {
        ll a=q,m=p;
        ll b=-lo[cur],n=mo[cur];
        if(n>m) swap(a,b),swap(m,n);
        // cerr<<m<<" "<<n<<"++\n";
        ll x,y,g=euclid(m,n,x,y);
        if((a-b)%g) return;
        x=(b-a)%n*x%n/g*m+a;
        // if(x<0) x+=m*n/g;
        dfs(cur-1,m*n/g,x,-coef);
    }
}
int main() {
    cin>>n>>c;
    // n=1e14,c=1e4;
    // ll aa=0;
    for(ll sf=0;sf<=10000000;++sf) {
        int ss[20],g=sf; sn=0;
        memset(ss,0,sizeof ss);
        while(g) ss[sn++]=g%10,g/=10;
        // cout<<sn<<"\n";
        // cerr<<sn<<" "<<pf<<"\n";
        for(ll rl=1,rlp=10;;++rl,rlp*=10) {
            if(sf>=rlp) continue;
            // cerr<<rl<<","<<rlp<<"+"<<sf<<"\n";
            if((sf+c)*rlp+sf>n) break;
            //x mod (sf+c)=0
            um=(n-sf)/rlp;
            ll po=1,low=0,rf=sf,rp=rlp;
            for(int o=rl-1;o>=0;--o) {
                // ++aa;
                //(x*po+low) mod (rf+c)=0
                // if(sf==12345&&rl==7)
                // cout<<sf<<","<<rl<<":"<<po<<","<<low<<","<<rf<<"\n";
                ::po[o]=po%(rf+c);
                ::lo[o]=low;
                ::mo[o]=rf+c;
                ::dc[o]=0;
                // cerr<<"mo:"<<::mo[o]<<"\n";
                // cerr<<rf+c<<"Jajan";
                rp/=10;
                rf-=rp*ss[o];
                po*=10; low=low*10+ss[o];
            }
            // assert(::po[rl-1]==0);
            dfs(rl-2,mo[rl-1],-lo[rl-1]);
        }
    }
    cout<<ans<<"\n";
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 178ms
memory: 3568kb

input:

20 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 179ms
memory: 3548kb

input:

111 4

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 178ms
memory: 3692kb

input:

1111 10

output:

75

result:

ok 1 number(s): "75"

Test #4:

score: 0
Accepted
time: 178ms
memory: 3692kb

input:

1000000 7

output:

111529

result:

ok 1 number(s): "111529"

Test #5:

score: 0
Accepted
time: 177ms
memory: 3612kb

input:

1 1

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 177ms
memory: 3600kb

input:

99999 10000

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 177ms
memory: 3688kb

input:

1000 1

output:

287

result:

ok 1 number(s): "287"

Test #8:

score: 0
Accepted
time: 178ms
memory: 3672kb

input:

10000000 1

output:

3102010

result:

ok 1 number(s): "3102010"

Test #9:

score: 0
Accepted
time: 179ms
memory: 3564kb

input:

100000000 1

output:

31035571

result:

ok 1 number(s): "31035571"

Test #10:

score: 0
Accepted
time: 181ms
memory: 3544kb

input:

1000000000 1

output:

310375697

result:

ok 1 number(s): "310375697"

Test #11:

score: 0
Accepted
time: 191ms
memory: 3628kb

input:

10000000000 1

output:

3103907933

result:

ok 1 number(s): "3103907933"

Test #12:

score: 0
Accepted
time: 221ms
memory: 3692kb

input:

100000000000 1

output:

31039265058

result:

ok 1 number(s): "31039265058"

Test #13:

score: 0
Accepted
time: 345ms
memory: 3596kb

input:

1000000000000 1

output:

310394177863

result:

ok 1 number(s): "310394177863"

Test #14:

score: 0
Accepted
time: 780ms
memory: 3568kb

input:

10000000000000 1

output:

3103943538348

result:

ok 1 number(s): "3103943538348"

Test #15:

score: 0
Accepted
time: 2256ms
memory: 3616kb

input:

100000000000000 1

output:

31039449626535

result:

ok 1 number(s): "31039449626535"

Test #16:

score: 0
Accepted
time: 2267ms
memory: 3628kb

input:

100000000000000 10

output:

9041696367054

result:

ok 1 number(s): "9041696367054"

Test #17:

score: 0
Accepted
time: 2242ms
memory: 3632kb

input:

100000000000000 100

output:

1744469671929

result:

ok 1 number(s): "1744469671929"

Test #18:

score: 0
Accepted
time: 2242ms
memory: 3608kb

input:

100000000000000 1000

output:

263959224215

result:

ok 1 number(s): "263959224215"

Test #19:

score: 0
Accepted
time: 2172ms
memory: 3616kb

input:

100000000000000 10000

output:

35400402243

result:

ok 1 number(s): "35400402243"

Test #20:

score: 0
Accepted
time: 2247ms
memory: 3628kb

input:

100000000000000 239

output:

870346971377

result:

ok 1 number(s): "870346971377"

Test #21:

score: 0
Accepted
time: 2240ms
memory: 3548kb

input:

99999987654321 111

output:

1606282527743

result:

ok 1 number(s): "1606282527743"

Test #22:

score: 0
Accepted
time: 2162ms
memory: 3564kb

input:

96709210826727 9568

output:

35605797003

result:

ok 1 number(s): "35605797003"

Test #23:

score: 0
Accepted
time: 1032ms
memory: 3608kb

input:

22952388910151 8412

output:

9470863043

result:

ok 1 number(s): "9470863043"

Test #24:

score: 0
Accepted
time: 1413ms
memory: 3688kb

input:

49191272026279 3065

output:

49381052989

result:

ok 1 number(s): "49381052989"

Test #25:

score: 0
Accepted
time: 1827ms
memory: 3564kb

input:

75434450109703 9205

output:

28741533023

result:

ok 1 number(s): "28741533023"

Test #26:

score: 0
Accepted
time: 415ms
memory: 3568kb

input:

1677628193127 753

output:

5631822336

result:

ok 1 number(s): "5631822336"

Test #27:

score: 0
Accepted
time: 179ms
memory: 3600kb

input:

1000010000 10000

output:

328824

result:

ok 1 number(s): "328824"

Extra Test:

score: 0
Extra Test Passed