QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#27533#2172. Trailing DigitsHakujitsu#AC ✓5ms3876kbC++3.3kb2022-04-09 18:34:242022-04-29 06:24:11

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-29 06:24:11]
  • 评测
  • 测评结果:AC
  • 用时:5ms
  • 内存:3876kb
  • [2022-04-09 18:34:24]
  • 提交

answer

// how to come up with such solution :(

// #pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
// #pragma GCC optimize("trapv")
#include<bits/stdc++.h>
 
#define int long long
// #define i128 long long
#define double long double
using namespace std;
 
#define rep(i,n) for (int i=0;i<(int)(n);++i)
#define rep1(i,n) for (int i=1;i<=(int)(n);++i)
#define range(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second

typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
int dx[]={1,1,2,2,-1,-1,-2,-2};
int dy[]={2,-2,1,-1,2,-2,1,-1};
// const int mod=1e9+7;
const int INF=LLONG_MAX;
const double EPS=1e-9;
const int N=500007;
const int K=360*10000;
const int mod[2]={998244353,234567899};
const int base[2]={12321,32123};
mt19937 rng(1234);

int b,d;
vi s;

vi mult(vi &s,int t){
    int n=sz(s);
    vi ans(n+18,0);
    int now=0;
    for (int i=0;i<n+18;++i){
        if (i<n) now=now+s[i]*t;
        ans[i]=now%10, now/=10;
    }
    while (!ans.back()) ans.pop_back();
    return ans;
}

bool cmp(vi &l,vi &r){ // l<=r?
    if (sz(l)<sz(r)) return 1;
    if (sz(l)>sz(r)) return 0;
    int n=sz(l);
    for (int i=n-1;i>-1;--i){
        if (l[i]<r[i]) return 1;
        if (l[i]>r[i]) return 0;
    }
    return 1;
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout.precision(15), cerr.precision(15);
    cin>>b>>d;
    string t;
    cin>>t;
    reverse(range(t));
    // rep(i,100000) t.pb('9');
    for (auto c:t) s.pb(c-'0');
    if (d==0){
        vi tt;
        int tmp=b;
        while (tmp) tt.pb(tmp%10), tmp/=10;
        int ans=0;
        while (1){
            int g=__gcd(b,10ll);
            if (g==1) break;
            b/=g;
            tt=mult(tt,10/g);
            if (cmp(tt,s)) {ans++; continue;}
            cout<<ans;
            return 0;
        }
        vi ttmp(sz(tt),0);
        rep(i,sz(tt)) ttmp[i]=s[sz(s)-sz(tt)+i];
        if (cmp(tt,ttmp)) cout<<ans+sz(s)-sz(tt);
        else cout<<ans+sz(s)-sz(tt)-1;
        return 0;
    }
    vi cur,cur9;
    cur.pb(0), cur9.pb(0);
    int ans=0;
    while (1){
        int g=__gcd(b,10ll),idx=-1;
        for (int i=0;i<10/g;++i){
            int fk=(ans?cur9[ans]+1:d);
            if ((9*b*i+fk)%10==0) idx=i;
        }
        if (idx==-1){
            cout<<ans;
            return 0;
        }
        auto add=[&](vi &s,int b,int k){
            int n=sz(s);
            s.resize(max(n,k)+18);
            int now=0;
            for (int i=k;i<max(n,k)+18;++i){
                now=now+b%10+s[i];
                if (now==0&&b==0) break;
                s[i]=now%10, now/=10, b/=10;
            }
            while (!s.back()) s.pop_back();
        };
        add(cur,b*idx,ans), add(cur9,9*b*idx,ans);
        b/=g;
        // cerr<<sz(s)<<endl;
        if (!cmp(cur,s)){
            cout<<ans;
            return 0;
        }
        ans++;
        // for (auto c:cur) cerr<<c<<" ";
        // cerr<<endl;
        // for (auto c:cur9) cerr<<c<<" ";
        // cerr<<endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 3828kb

input:

583764 4 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500...

output:

9994

result:

ok single line: '9994'

Test #2:

score: 0
Accepted
time: 3ms
memory: 3848kb

input:

583764 2 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500...

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 3ms
memory: 3716kb

input:

583764 0 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500...

output:

9994

result:

ok single line: '9994'

Test #4:

score: 0
Accepted
time: 3ms
memory: 3708kb

input:

128 2 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500733...

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 3ms
memory: 3856kb

input:

128 0 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500733...

output:

9999

result:

ok single line: '9999'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3716kb

input:

128 8 558022069184230038938963154373006263760571458771657180531616870174538708073028810782561047069639027608389048778567660978632940168163019291290879822337556840316474756306063761657619759773088336245228734458838815045778851841542788114979050385904301768656279094511344874294029141687083122077500733...

output:

3

result:

ok single line: '3'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

3 3 55802206918423003893896315437300626376057145877165718053161687017453870807302881078256104706963902760838904877856766097863294016816301929129087982233755684031647475630606376165761975977308833624522873445883881504577885184154278811497905038590430176865627909451134487429402914168708312207750073308...

output:

10000

result:

ok single line: '10000'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3832kb

input:

2 2 55802206918423003893896315437300626376057145877165718053161687017453870807302881078256104706963902760838904877856766097863294016816301929129087982233755684031647475630606376165761975977308833624522873445883881504577885184154278811497905038590430176865627909451134487429402914168708312207750073308...

output:

10000

result:

ok single line: '10000'

Test #9:

score: 0
Accepted
time: 3ms
memory: 3712kb

input:

1 1 1

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 3ms
memory: 3624kb

input:

1 2 1

output:

0

result:

ok single line: '0'

Test #11:

score: 0
Accepted
time: 3ms
memory: 3572kb

input:

999999 1 999999

output:

0

result:

ok single line: '0'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3720kb

input:

999999 0 999999

output:

0

result:

ok single line: '0'

Test #13:

score: 0
Accepted
time: 5ms
memory: 3680kb

input:

333333 9 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

9996

result:

ok single line: '9996'

Test #14:

score: 0
Accepted
time: 0ms
memory: 3808kb

input:

333333 9 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

9996

result:

ok single line: '9996'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

44 3 615164488012970354555742215744

output:

0

result:

ok single line: '0'

Test #16:

score: 0
Accepted
time: 3ms
memory: 3676kb

input:

8 0 9999999999999999999

output:

18

result:

ok single line: '18'

Test #17:

score: 0
Accepted
time: 3ms
memory: 3664kb

input:

8 2 9999999999999999999

output:

1

result:

ok single line: '1'

Test #18:

score: 0
Accepted
time: 3ms
memory: 3716kb

input:

8 4 9999999999999999999

output:

2

result:

ok single line: '2'

Test #19:

score: 0
Accepted
time: 3ms
memory: 3572kb

input:

8 6 9999999999999999999

output:

1

result:

ok single line: '1'

Test #20:

score: 0
Accepted
time: 4ms
memory: 3664kb

input:

8 8 9999999999999999999

output:

19

result:

ok single line: '19'

Test #21:

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

input:

262144 0 9999999999999999999

output:

18

result:

ok single line: '18'

Test #22:

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

input:

262144 2 9999999999999999999

output:

1

result:

ok single line: '1'

Test #23:

score: 0
Accepted
time: 4ms
memory: 3572kb

input:

262144 4 9999999999999999999

output:

2

result:

ok single line: '2'

Test #24:

score: 0
Accepted
time: 3ms
memory: 3652kb

input:

262144 6 9999999999999999999

output:

1

result:

ok single line: '1'

Test #25:

score: 0
Accepted
time: 3ms
memory: 3624kb

input:

262144 8 9999999999999999999

output:

3

result:

ok single line: '3'

Test #26:

score: 0
Accepted
time: 0ms
memory: 3712kb

input:

625 0 1000

output:

0

result:

ok single line: '0'

Test #27:

score: 0
Accepted
time: 1ms
memory: 3644kb

input:

625 0 10000

output:

4

result:

ok single line: '4'

Test #28:

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

input:

625 0 10000000000

output:

10

result:

ok single line: '10'

Test #29:

score: 0
Accepted
time: 3ms
memory: 3572kb

input:

625 5 1000

output:

1

result:

ok single line: '1'

Test #30:

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

input:

625 5 10000

output:

1

result:

ok single line: '1'

Test #31:

score: 0
Accepted
time: 2ms
memory: 3728kb

input:

625 5 10000000000

output:

1

result:

ok single line: '1'

Test #32:

score: 0
Accepted
time: 0ms
memory: 3716kb

input:

416113 2 910206714449570543178771288072

output:

24

result:

ok single line: '24'

Test #33:

score: 0
Accepted
time: 3ms
memory: 3720kb

input:

155727 8 595222817557156175409864490146

output:

24

result:

ok single line: '24'

Test #34:

score: 0
Accepted
time: 3ms
memory: 3624kb

input:

482185 7 175891766468946506850940294915

output:

0

result:

ok single line: '0'

Test #35:

score: 0
Accepted
time: 3ms
memory: 3736kb

input:

809543 9 706310782306606357041238521573

output:

24

result:

ok single line: '24'

Test #36:

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

input:

784349 2 140172854681543411198107462791

output:

23

result:

ok single line: '23'

Test #37:

score: 0
Accepted
time: 3ms
memory: 3580kb

input:

884771 6 878053904682776335641403365951

output:

24

result:

ok single line: '24'

Test #38:

score: 0
Accepted
time: 3ms
memory: 3720kb

input:

285115 7 510637551593904109970235924750

output:

0

result:

ok single line: '0'

Test #39:

score: 0
Accepted
time: 2ms
memory: 3836kb

input:

446344 8 903873563961494937101879656386579063234122909426702463868191198982245137424039427440175427556962397041118420136726887092853026726934104808566629597404508140480575992676594274115233279041150264028795434104067914849478866456192407793002021965833468079778630255143444525376989079382208643362458...

output:

9995

result:

ok single line: '9995'

Test #40:

score: 0
Accepted
time: 5ms
memory: 3748kb

input:

217176 8 746720628343704092031643549114635655136791126811051782038588698583562173486474660147736305824479303770760232163768352183708565173054518463256824577064413625680194290063831377238862971071846843280498770970154459912493995293604486306696113122808160233657375115399216282414502057456592999985641...

output:

9995

result:

ok single line: '9995'

Test #41:

score: 0
Accepted
time: 5ms
memory: 3832kb

input:

591976 8 997688187554288608289696884444974394245886896647212441828767818311997092160967371255532165988508545021286506736704730143980876547206426443074075450563958052385014127642212294228193051916496652316016549674647007259802903947613096796599924760429663473913319454783603356257452483582255380157929...

output:

9995

result:

ok single line: '9995'

Test #42:

score: 0
Accepted
time: 5ms
memory: 3852kb

input:

308945 5 962476931095598250044305868135885243557388313522332291867917574431355233922993304117647070995131553330332941633745363521615864814876653718068119371777377239796738753166939860072333764723729655459600360732826690810931322099300456303655259230988921806364953099639264334432713159446169102673437...

output:

9995

result:

ok single line: '9995'

Test #43:

score: 0
Accepted
time: 2ms
memory: 3684kb

input:

953215 5 481379773305937010787000848653505393811784682453377327931890614700475555426776917124183304939480619234810630040231631970261984136707831904841144438858481007543348015107439662369399747861912671557090065865016622444295889867974657697201792908751777382148402012154785104292434498920894247193558...

output:

9994

result:

ok single line: '9994'

Test #44:

score: 0
Accepted
time: 5ms
memory: 3876kb

input:

711785 5 624522418749983727962202416596678244083668011938141912561864354048413105915711234753191969044814346136404671485469428764114784402574391892322067753961421343947532626292641042989488918633658784104942163903053502260790194000740256301403470440332255977429369236483168636332974165475448631524588...

output:

9994

result:

ok single line: '9994'