QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105111#983. The Hash TablechenshiWA 1ms1760kbC++1.2kb2023-05-13 00:27:542023-05-13 00:27:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-13 00:27:56]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:1760kb
  • [2023-05-13 00:27:54]
  • 提交

answer

#include<cstdio>
using namespace std;
int T,n,m,pri[9999],cnt,pw[9999],v[9999];long long ans;
long long calc(int n,int a,int b,int c){
	if(!a||!n) return b/c*1ll*n;
	if(a>=c||b>=c) return calc(n,a%c,b%c,c)+b/c*1ll*n+a/c*(n*(n-1ll)/2);
	int t=(a*1ll*n+b)/c;
	return n*1ll*t-calc(t,c,c+a-b-1,a);
}
inline int Calc(int n,int a,int b,int c,int t){
	if(a<0) b+=(n-1)*a,a=-a;
	if(b<0){
		if(!a) return 0;
		int tmp=(-b+a-1)/a;
		n-=tmp;b+=a*tmp;
	}
	return (n>0)?calc(n,a,b,c)+t*n:0;
}
void dfs(int nw,int a,int b,int coef){
	if(nw>cnt){
		for(int i=0,t1,t2;i<2;++i) for(int j=0;j<2;++j) if((i*a+j*b)%2==0){
			t1=(n-1)/a-i;t2=(n-1)*2/a-i;t1>>=1;t2>>=1;
			if(t1>=0) ans+=coef*(Calc(t1+1,a,(i*a-j*b)/2,b,j)-Calc(t1+1,-a,n-1-(i*a+j*b)/2,b,j));
			if(t2>=0) ans+=coef*Calc(t2+1,-a,n-1-(i*a+j*b)/2,b,j);
		}
		return;
	}
	for(int i=0,t=1;i<=pw[nw];++i,t*=pri[nw]){
		dfs(nw+1,a*t,b*v[nw]/t,coef);
		if(i<pw[nw]) dfs(nw+1,a*t*pri[nw],b*v[nw]/t,-coef);
	}
}
int main(){
	for(scanf("%d",&T);T--;printf("%lld\n",ans),ans=cnt=0){
		scanf("%d%d",&n,&m);
		for(int i=2;i*i<=m;++i) if(m%i==0){
			pri[++cnt]=i;pw[cnt]=0;v[cnt]=1;
			for(;m%i==0;m/=i) ++pw[cnt],v[cnt]*=i;
		}
		if(m>1) pri[++cnt]=m,pw[cnt]=1,v[cnt]=m;
		dfs(1,1,1,1);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 1608kb

input:

3
5 4
1234 5678
5 4

output:

4
229
4

result:

ok 3 tokens

Test #2:

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

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: -100
Wrong Answer
time: 0ms
memory: 1500kb

input:

5
351177178 2
236814804 3
406487669 4
107706571 5
206252441 6

output:

-1603436932
-286774082
-46040710876
-292471569
22639066363

result:

wrong answer 1st words differ - expected: '30831352411422332', found: '-1603436932'