QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19582 | #983. The Hash Table | CharlieVinnie | AC ✓ | 412ms | 3660kb | C++14 | 3.7kb | 2022-02-06 10:27:47 | 2022-05-06 06:11:06 |
Judging History
answer
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define clr(a,v) memset(a,v,sizeof(a))
#define Fin(file) freopen(file".in","r",stdin)
#define Fout(file) freopen(file".out","w",stdout)
#define Fgen(file) freopen(file".in","w",stdout)
#define Fans(file) freopen(file".ans","w",stdout)
#define int long long
#define div Charlie_Vinnie
using namespace std;
const int N=1e5+5;
int div(int x,int y)
{
return x>=0 ? x/y : -((-x-1)/y+1);
}
// sum(i=0..n){(a*i+c)/b}
int EuF(int n,int a,int b,int c)
{
if(n<0) return 0;
if(a==0) return (n+1)*(c/b);
if(a>=b || c>=b){
return (a/b)*(n*(n+1)/2)+(c/b)*(n+1)+EuF(n,a%b,b,c%b);
}
else{
int m=(a*n+c)/b-1;
return n*(m+1)-EuF(m,b,a,b-c-1);
}
}
// allows a,b,c<0, but must n>=0
int exEuF(int n,int a,int b,int c)
{
if(b<0) { a=-a; b=-b; c=-c; }
int res=0;
if(a<0){
res-=(-div(a,b))*(n*(n+1)/2);
a=(a%b+b)%b;
}
if(c<0){
res-=(-div(c,b))*(n+1);
c=(c%b+b)%b;
}
return res+EuF(n,a,b,c);
}
// sum(i=l..r){(a*i+c)/b}
int sum(int l,int r,int a,int b,int c)
{
if(l>0){
return exEuF(r,a,b,c)-exEuF(l-1,a,b,c);
}
else if(r<0){
return exEuF(-l,-a,b,c)-exEuF(-r-1,-a,b,c);
}
else{
return exEuF(r,a,b,c)+exEuF(-l,-a,b,c)-div(c,b);
}
}
// num of (u,v) so that t1<ua+vb<=n+t1, t2<ua-vb<=n+t2
int calc(int n,int a,int b,int t1,int t2)
{
int mid=div(t1-t2,2*b);
int lt=div(t1-t2-n-1,2*b);
int rt=div(n+t1-t2,2*b);
return sum(lt+1,mid,b,a,n+t2)-sum(lt+1,mid,-b,a,t1)+sum(mid+1,rt,-b,a,n+t1)-sum(mid+1,rt,b,a,t2);
}
// sum(0<=x<y<n){[a|x+y][b|x-y]}
int work(int n,int a,int b)
{
int res=0;
res+=calc(n,a,b,-1,-1);
if(b%2==0) res+=calc(n,a,b,-b/2-1,b/2-1);
if(a%2==0) res+=calc(n,a,b,-a/2-1,-a/2-1);
if((a+b)%2==0) res+=calc(n,a,b,-(a+b)/2-1,(b-a)/2-1);
// now res = sum(x=0..n-1,y=0..n-1){[a|x+y][b|x-y]}
if(a%2==0) res-=(n-1)/(a/2)+1;
else res-=(n-1)/a+1;
res/=2;
// printf("work(%lld,%lld,%lld)\n",n,a,b);
// int hh=0;
// For(x,0,n-1) For(y,x+1,n-1) if((x+y)%a==0&&(x-y)%b==0) printf("(%lld,%lld) ",x,y),hh++;
// assert(res==hh);
// cout<<"res="<<res<<endl;
return res;
}
int n,m,ans;
int p[25],w[25],pw[25][45],cnt;
void dfs(int d,int x,int y,int c)
{
if(d==cnt+1){
// printf("%cwork(%lld,%lld,%lld)\n",c==1?'+':'-',n,x,y);
// printf("%c ",c==1?'+':'-');
ans+=c*work(n,x,y);
return;
}
For(i,0,w[d]){
dfs(d+1,x*pw[d][i],y*pw[d][w[d]-i],c);
if(i!=w[d]) dfs(d+1,x*pw[d][i+1],y*pw[d][w[d]-i],-c);
}
}
void solve()
{
cin>>n>>m;
cnt=0;
int sqm=sqrt(m+0.5),t=m;
For(i,2,sqm){
if(t%i==0){
p[++cnt]=i; w[cnt]=0;
while(t%i==0) { w[cnt]++; t/=i; }
pw[cnt][0]=1;
For(j,1,w[cnt]+1) pw[cnt][j]=pw[cnt][j-1]*p[cnt];
}
}
if(t>1){
p[++cnt]=t; w[cnt]=1;
pw[cnt][0]=1; pw[cnt][1]=t; pw[cnt][2]=t*t;
}
ans=0;
dfs(1,1,1,1);
cout<<ans<<endl;
// int hh=0;
// For(x,0,n-1) For(y,x+1,n-1) if((x+y)*(x-y)%m==0) printf("(%lld,%lld)\n",x,y),hh++;
// printf("std = %lld\n",hh);
}
signed main()
{
int T;
cin>>T;
while(T--) solve();
cerr<<"Time = "<<clock()<<" ms"<<endl;
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 3652kb
input:
3 5 4 1234 5678 5 4
output:
4 229 4
result:
ok 3 tokens
Test #2:
score: 0
Accepted
time: 6ms
memory: 3588kb
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: 1ms
memory: 3592kb
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: 345ms
memory: 3660kb
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: 407ms
memory: 3568kb
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: 412ms
memory: 3520kb
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: 7ms
memory: 3640kb
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: 4ms
memory: 3524kb
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: 8ms
memory: 3564kb
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: 13ms
memory: 3540kb
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: 3ms
memory: 3568kb
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: 4ms
memory: 3452kb
input:
5 943210366 900720143 752627016 900720143 905995005 942060233 775132721 942060233 821646602 962178361
output:
1522977645 931104902 1306217667 941841841 691366758
result:
ok 5 tokens