QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190487#6640. Talk That TalkCrysflyTL 777ms65544kbC++202.9kb2023-09-28 23:10:012023-09-28 23:10:01

Judging History

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

  • [2023-09-28 23:10:01]
  • 评测
  • 测评结果:TL
  • 用时:777ms
  • 内存:65544kb
  • [2023-09-28 23:10:01]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define int long long
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 4000015
#define inf 0x3f3f3f3f

ll p,res;
int t;
long double iP;
ll mul(ll x,ll y){
	ll d=x*iP*y+0.5,s=x*y-d*p;
	return s+((s>>63)&p);
}
ll qpow(ll a,ll b){
	ll c=1;
	for(;b;b>>=1,a=mul(a,a))if(b&1)c=mul(c,a);
	return c;
}

int A[maxn],SA[maxn],*a=A+2000003,*sa=SA+2000003;
void work()
{
	p=read(),t=read(),iP=1.0l/p;
	t=min(t,p/2-1),res=0;
	
	int n=t*2;
//	n=p-1;
	n=min(n,p-1);
	Rep(i,n,-n){
		if(i<=0)a[i]=(p%4==3?-a[-i]:a[-i]);
		else a[i]=(qpow(i,(p-1)/2)==1)?1:-1;
	}
	a[-n-1]=sa[-n-1]=0;
	For(i,-n,n)sa[i]=sa[i-1]+a[i];
	
//	For(d,1,t){
//		int sum=0,sum2=0;
//		For(i,0,p-1){
//			int now=a[i]*a[(i+d)%p]+(a[(i+d)%p]*a[(i+d*2)%p])+(a[i]*a[(i+2*d)%p]);
//			now+=1;
////			cerr<<"now "<<now<<"\n";
//			sum+=a[i]*a[(i+d)%p],sum+=(a[(i+d)%p]*a[(i+d*2)%p]),sum2+=(a[i]*a[(i+2*d)%p]);
//		}
//	//	sum=sum*2+sum2;
//		sum+=sum2;
//		sum+=p;
//		cout<<sum<<"\n";
//	//	sum-=(a[d]*a[(p-d)%p]+1);
//	//	cout<<"S "<<sum<<"\n";
//		res+=sum;
//	}
	res+=(p-3)*t;
	For(d,1,t) res-=d*2;
//	cout<<"RES "<<res<<"\n";
//	For(d,1,t){
//	//	res-=d*2;
//		For(i,0,p-1){
//			if(i+d*2>=p){
//				int now=a[i]*a[(i+d)%p]+(a[(i+d)%p]*a[(i+d*2)%p])+(a[i]*a[(i+2*d)%p]);
//	//			cout<<"Sub "<<now<<"\n";
//				res-=now;
//			}
//		}
//	}

	auto pres=[&](int l,int r){
		assert(r<=n);
		assert(l>=-n);
		return sa[r]-sa[l-1];
	};
	res-=t;
	cerr<<"Res "<<res<<"\n";
	
	For(i,-2*t,-1){
		int j=(-i+1)/2;
//		cout<<"i,j "<<i<<" "<<j<<"\n";
		res-=a[i]*pres(i+j,i+t);
	}
	For(i,1,t){
		res-=a[i]*pres(i+i,i+t);
	}
//	cout<<"qwq "<<res<<"\n";
	For(i,0,t-1)
		res-=a[i-t]*pres(0,i);
//	cout<<"qwq2 "<<res<<"\n";
	
	sa[-n-1]=sa[-n-2]=0;
	For(i,-n,n){
		sa[i]=a[i];
		if(i-2>=-n) sa[i]+=sa[i-2];
	}
	For(i,-2*t,-1){
		int j=(-i+1)/2;
//		cout<<"i,j "<<i<<" "<<j<<" "<<a[i]<<" "<<sa[i+2*t]<<" "<<sa[i+2*(j-1)]<<"\n";
		res-=a[i]*(sa[i+2*t]-sa[i+2*(j-1)]);
	//	cout<<"ovo "<<a[i]*(sa[i+2*t]-sa[i+2*(j-1)])<<"\n";
		//cout<<"res: "<<res<<"\n";
	}
	
	cerr<<"res "<<res<<"\n";
	res/=4;
	cout<<res<<"\n";
}

signed main()
{
	int T=read();
	while(T--)work();
	return 0;
}
/*
7
7 32
13 1
13 2
67 11
2003 44
1000003 1984
999999999989 987654
*/

详细

Test #1:

score: 100
Accepted
time: 777ms
memory: 65544kb

input:

7
7 32
13 1
13 2
67 11
2003 44
1000003 1984
999999999989 987654

output:

0
2
2
146
21510
495014784
246913256130162788

result:

ok 7 numbers

Test #2:

score: 0
Accepted
time: 147ms
memory: 7540kb

input:

11696
5 1
5 2
7 1
7 2
7 3
11 1
11 2
11 3
11 4
11 5
13 1
13 2
13 3
13 4
13 5
13 6
17 1
17 2
17 3
17 4
17 5
17 6
17 7
17 8
19 1
19 2
19 3
19 4
19 5
19 6
19 7
19 8
19 9
23 1
23 2
23 3
23 4
23 5
23 6
23 7
23 8
23 9
23 10
23 11
29 1
29 2
29 3
29 4
29 5
29 6
29 7
29 8
29 9
29 10
29 11
29 12
29 13
29 14
31...

output:

0
0
0
0
0
2
4
4
6
6
2
2
4
4
4
4
2
4
4
6
6
6
8
8
4
8
10
12
16
18
18
20
20
4
8
12
16
20
22
24
24
24
24
24
6
12
18
24
24
28
32
36
40
40
40
44
44
44
6
12
18
22
26
32
34
36
42
44
44
46
46
46
46
8
14
22
26
32
38
42
44
52
54
56
60
62
62
64
64
64
64
8
16
20
28
34
38
44
52
54
56
60
64
66
68
70
74
74
74
76
76...

result:

ok 11696 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

500000
71 1
41 1
67 1
17 1
29 1
97 1
11 1
59 1
89 1
71 1
7 1
31 1
71 1
13 1
67 1
97 1
13 1
37 1
29 1
13 1
67 1
37 1
97 1
59 1
89 1
83 1
73 1
29 1
13 1
89 1
17 1
43 1
31 1
37 1
79 1
41 1
23 1
83 1
7 1
19 1
11 1
67 1
73 1
71 1
67 1
17 1
47 1
17 1
29 1
41 1
97 1
13 1
47 1
43 1
23 1
7 1
73 1
13 1
47 1
7...

output:

16
8
16
2
6
22
2
14
20
16
0
6
16
2
16
22
2
8
6
2
16
8
22
14
20
20
16
6
2
20
2
10
6
8
18
8
4
20
0
4
2
16
16
16
16
2
10
2
6
8
22
2
10
10
4
0
16
2
10
0
2
20
6
8
14
16
16
2
14
12
2
8
16
0
16
0
18
2
2
4
16
14
16
2
2
22
14
20
20
10
16
16
22
14
16
14
18
0
20
6
2
16
10
4
14
2
22
4
20
2
18
0
6
20
2
4
14
0
12...

result: