QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#495813 | #9139. Prefix Divisible by Suffix | ucup-team191# | TL | 2578ms | 30208kb | C++23 | 2.9kb | 2024-07-27 23:52:52 | 2024-07-27 23:52:55 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)
const int N=300010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;
ll euclid(ll a, ll b, ll &x, ll &y) {
if(!b) return x = 1, y = 0, a;
if(a < b) return euclid(b, a, y, x);
ll xx = 1; x = 0;
ll yy = 0; y = 1;
while(1) {
ll q = a / b;
a -= q * b;
swap(a, b);
if(!b) break;
ll xxx = x, yyy = y;
x = xx - x * q;
y = yy - y * q;
xx = xxx;
yy = yyy;
}
return a;
}
ll crt(ll a, ll m, ll b, ll n) { //assumes nm<=1e18
if (n > m) swap(a, b), swap(m, n);
ll x, y, g = euclid(m, n, x, y);
assert((a - b) % g == 0); // else no solution
x = (b - a) % n * x % n / g * m + a;
return x < 0 ? x + m/g*n : x;
}
ll inv(ll x,ll mod)
{
ll a,b;
euclid(x,mod,a,b);
a%=mod;
if (a<0) a+=mod;
return a;
}
ll n,c,cn[20][N];
int sufr[20],dig[20];
void addRes(ll&u,ll&mo,ll ma,ll mu,ll dod,ll nm) //nm<=suf+c<1e9, mo<=ma, ma*nm<=1e18 -> mo*nm<=1e18
{
if (mo==ma)
{
if (((u%nm)*mu+dod)%nm)
{
u=-1;
}
return;
}
if (dod==0 && nm==mo) return;
ll g=gcd(mu,nm);
if (dod%g)
{
u=-1;
return;
}
mu/=g;
nm/=g;
dod/=g;
ll req=((nm-dod%nm)*inv(mu%nm,nm))%nm;
if ((u-req)%gcd(mo,nm))
{
u=-1;
return;
}
ll re=crt(u,mo,req,nm);
if (re>ma)
{
u=-1;
return;
}
u=re;
mo=min(ma,lcm(mo,nm));
}
ll leqmo(ll x,ll u,ll m)
{
if (u==0) return x/m;
else return (x+m-u)/m;
}
void rek(int i,int mask,ll u,ll mo,ll ma,ll mul,ll dod)
{
if (i==0)
{
//cout<<i<<' '<<mask<<' '<<u<<' '<<mo<<' '<<ma<<en;
ll l=1,r=10,lp=1;
int lsuf=__lg(mask);
while (l<=ma) //l .. r-1
{
cn[lp+lsuf][mask]+=leqmo(min(r-1,ma),u,mo)-leqmo(l-1,u,mo);
l*=10;
r*=10;
++lp;
}
return;
}
rek(i-1,mask,u,mo,ma,mul*10,dod*10+dig[i-1]);
addRes(u,mo,ma,mul,dod,sufr[i]+c);
if (u==-1) return;
rek(i-1,mask+(1<<i),u,mo,ma,mul*10,dod*10+dig[i-1]);
//if (r<=ma) rek(i-1,mask+(1<<i),r,ma);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>c;
//n=1e14;
//c=1;
for (int suf=0;suf<1e7;++suf)
{
memset(dig,0,sizeof(dig));
ll cc=suf,l=0,p10=1;
while (cc)
{
dig[l]=cc%10;
cc/=10;
++l;
p10*=10;
}
ll p101=10;
for (int i=1;i<=15;++i)
{
sufr[i]=suf%p101;
p101*=10;
}
for (int l0=0;;++l0,p10*=10)
{
if (l+l0==0) continue;
ll os=n/p10;
if (suf>n%p10) --os;
ll rr=suf+c;
if (rr>os) break;
rek(l+l0-1,1<<(l+l0),0,rr,os,10,dig[l+l0-1]);
}
}
ll odd=0;
for (int le=1;le<=15;++le)
{
ll cu=0;
for (int b=1;b<(1<<le);++b)
{
if (__builtin_popcount(b)%2) cu+=cn[le][b];
else cu-=cn[le][b];
}
//cout<<le<<' '<<cu<<en;
odd+=cu;
}
cout<<odd<<en;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 460ms
memory: 5608kb
input:
20 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 461ms
memory: 7708kb
input:
111 4
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: 0
Accepted
time: 456ms
memory: 9828kb
input:
1111 10
output:
75
result:
ok 1 number(s): "75"
Test #4:
score: 0
Accepted
time: 463ms
memory: 15900kb
input:
1000000 7
output:
111529
result:
ok 1 number(s): "111529"
Test #5:
score: 0
Accepted
time: 461ms
memory: 3704kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 459ms
memory: 3548kb
input:
99999 10000
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 461ms
memory: 9792kb
input:
1000 1
output:
287
result:
ok 1 number(s): "287"
Test #8:
score: 0
Accepted
time: 458ms
memory: 17884kb
input:
10000000 1
output:
3102010
result:
ok 1 number(s): "3102010"
Test #9:
score: 0
Accepted
time: 461ms
memory: 19936kb
input:
100000000 1
output:
31035571
result:
ok 1 number(s): "31035571"
Test #10:
score: 0
Accepted
time: 469ms
memory: 22120kb
input:
1000000000 1
output:
310375697
result:
ok 1 number(s): "310375697"
Test #11:
score: 0
Accepted
time: 515ms
memory: 24112kb
input:
10000000000 1
output:
3103907933
result:
ok 1 number(s): "3103907933"
Test #12:
score: 0
Accepted
time: 605ms
memory: 26152kb
input:
100000000000 1
output:
31039265058
result:
ok 1 number(s): "31039265058"
Test #13:
score: 0
Accepted
time: 1262ms
memory: 28280kb
input:
1000000000000 1
output:
310394177863
result:
ok 1 number(s): "310394177863"
Test #14:
score: 0
Accepted
time: 2578ms
memory: 30208kb
input:
10000000000000 1
output:
3103943538348
result:
ok 1 number(s): "3103943538348"
Test #15:
score: -100
Time Limit Exceeded
input:
100000000000000 1