QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#401886 | #983. The Hash Table | grass8cow | AC ✓ | 237ms | 3928kb | C++17 | 2.0kb | 2024-04-29 16:07:36 | 2024-04-29 16:07:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int p[40],c[40],e;
void fj(int n){
e=0;
for(int i=2;i*i<=n;i++)if(!(n%i)){
p[++e]=i,c[e]=0;
while(!(n%i))n/=i,c[e]++;
}
if(n>1)p[++e]=n,c[e]=1;
}
#define ll long long
__int128 ans;
ll n,m;
ll sq(ll a,ll b){if(a<=0)return a/b;return (a-1)/b+1;}
ll xq(ll a,ll b){if(a<0)return (a+1)/b-1;return a/b;}
struct nn{ll a,b,c;};
nn operator + (const nn &x,const nn &y){
return (nn){x.a+y.a,x.b+y.b,x.c+y.c+x.a*y.b};
}
nn qpow(nn a,ll b){
nn c=(nn){0,0,0};
for(;b;b>>=1){
if(b&1)c=c+a;
a=a+a;
}
return c;
}
nn F(ll a,ll b,ll c,ll n,nn x,nn y){
if(!n){return (nn){0,0,0};}
if(a>=c)return F(a%c,b,c,n,x,qpow(x,a/c)+y);
ll cz=(a*n+b)/c;
if(!cz)return qpow(y,n);
return qpow(y,(c-b-1)/a)+x+F(c,(c-b-1)%a,a,cz-1,y,x)+qpow(y,n-(c*cz-b-1)/a);
}
ll vp(ll a,ll b,ll n,ll c){
if(a>b)swap(a,b);
if(!n)b=0;else b=(b-a)/n;
swap(a,b);
nn fk=qpow({1,0,0},b/c)+F(a,b%c,c,n,{1,0,0},{0,1,0});
return fk.c+b/c;
}
ll calc(ll a,ll b){
ll s=0;
ll o=b;if(!(o&1))o/=2;
s-=(n-1)/o+1;
//o|i,i<n
for(int c=0;c<2;c++)for(int d=0;d<2;d++){
if((c*a+d*b)%2)continue;
ll N=(n-1)*2-b*d-c*a,M=a*c-d*b;
if(N<0)continue;N/=2,M/=2;
ll bl=sq(M,b),br=xq(N,b);
if(bl>br)continue;
ll tz=xq(N+M,b*2);
s+=br-bl+1;
if(tz>=bl){
ll e=min(br,tz);
s+=vp(b*bl-M,b*e-M,e-bl,a);
}
if(tz<br)tz=max(tz+1,bl),s+=vp(N-b*tz,N-b*br,br-tz,a);
}
return s;
//a|y-x,b|y+x
}
void dfs(int x,int z1,int z2,int t){
if(x==e+1){ans+=calc(z2,m/z1)*t;return;}
int o=1;
for(int i=0;i<=c[x];i++){
if(i)o*=p[x];
dfs(x+1,z1*o,z2*o,t);
if(i<c[x])dfs(x+1,z1*o,z2*o*p[x],-t);
}
}
void sol(){
scanf("%lld%lld",&n,&m);fj(m);
ans=0,dfs(1,1,1,1),printf("%lld\n",(long long)ans);
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3928kb
input:
3 5 4 1234 5678 5 4
output:
4 229 4
result:
ok 3 tokens
Test #2:
score: 0
Accepted
time: 1ms
memory: 3884kb
input:
5 919191919 998244353 919191919 308308924 124312512 700980343 199712020 199712020 1000000000 1000000000
output:
420069742 18975162173 34523625 619107226 36400000000
result:
ok 5 tokens
Test #3:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
5 351177178 2 236814804 3 406487669 4 107706571 5 206252441 6
output:
30831352411422332 15578125268692158 41308056059019556 2088126907497711 5908342860201211
result:
ok 5 tokens
Test #4:
score: 0
Accepted
time: 190ms
memory: 3872kb
input:
5 795965436 698377680 775372176 735134400 891189723 555660000 255414898 555660000 649803503 967458816
output:
252131828509 252898591532 196092107465 16019483439 23110007395
result:
ok 5 tokens
Test #5:
score: 0
Accepted
time: 206ms
memory: 3860kb
input:
5 980056074 669278610 786804145 536870912 94743614 669278610 925596206 551350800 277753133 735134400
output:
151494104505 16332684795 1386109471 391665329539 32367779875
result:
ok 5 tokens
Test #6:
score: 0
Accepted
time: 237ms
memory: 3876kb
input:
5 407928831 901800900 959460685 551350800 929921323 901800900 880362378 551350800 169887581 551350800
output:
24176516170 420863755861 126194540248 354298688005 13128893811
result:
ok 5 tokens
Test #7:
score: 0
Accepted
time: 2ms
memory: 3844kb
input:
5 755923335 903960666 248398318 89883150 32020160 729647777 48000958 745177909 261977197 120541067
output:
5122927135 10499768724 1250410 5736978 980686184
result:
ok 5 tokens
Test #8:
score: 0
Accepted
time: 1ms
memory: 3812kb
input:
5 120618885 904615503 860299168 970357266 2922308 99586084 116144876 802899228 621873179 98196855
output:
72362680 1890408813 77476 180492837 23279216759
result:
ok 5 tokens
Test #9:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
5 337929091 905270340 472298323 850733078 973824456 50926103 184354331 860587779 981801929 75885412
output:
2695205304 791722210 18137207967 13641011 50173808550
result:
ok 5 tokens
Test #10:
score: 0
Accepted
time: 6ms
memory: 3884kb
input:
5 725900160 906132534 140660629 407214962 935092856 84357639 706890499 221743500 461717852 516104007
output:
3448528660 288899219 61675795925 96294825272 475220310
result:
ok 5 tokens
Test #11:
score: 0
Accepted
time: 1ms
memory: 3924kb
input:
5 943210366 906787371 752627016 287590774 905995005 183214075 775132721 279464819 821646602 493792564
output:
11587484479 1606204266 21788486368 3802176853 10347608659
result:
ok 5 tokens
Test #12:
score: 0
Accepted
time: 1ms
memory: 3888kb
input:
5 943210366 900720143 752627016 900720143 905995005 942060233 775132721 942060233 821646602 962178361
output:
1522977645 931104902 1306217667 941841841 691366758
result:
ok 5 tokens