QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149289 | #6640. Talk That Talk | qzez | WA | 1853ms | 50356kb | C++14 | 2.1kb | 2023-08-24 12:47:04 | 2023-08-24 12:47:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
using big=__int128;
const int N=2e6+10;
ll p;
int T,n,t;
ll qpow(big x,ll y,ll mod){
big ans=1;
for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
return ans;
}
bool chk(ll x,ll p){
return qpow(x,(p-1)/2,p)==1;
}
int a[N],b[N],c[N*2],s[N*2];
ll calc(ll p){
int cnt=chk(p-2,p)==chk(p-1,p)&&!chk(p-1,p);
if(p%4==3)return p/4-cnt;
else if(p%8==5)return p/4-1-cnt;
else return p/4-2-cnt;
}
ll solve(int *a,int n){
// ll sum=0;
// for(int i=0;i<n;i++){
// for(int t=1;i-t>=0&&i+t<n;t++){
// sum+=a[i]*a[i-t]+a[i]*a[i+t]+a[i-t]*a[i+t]+1;
// }
// }
// debug(ary(a,0,n-1),sum);
// return sum;
for(int i=0;i<n;i++){
s[i]=(i?s[i-1]:0)+a[i];
}
auto calc=[&](int l,int r){
if(l-->r)return 0;
return (r>=0?s[r]:0)-(l>=0?s[l]:0);
};
ll ans=0;
for(int i=0;i<n;i++){
int x=min({t,i,n-i-1});
ans+=a[i]*(calc(i-x,i-1)+calc(i+1,i+x));
}
// debug(ans);
for(int i=0,x=0;i<n;i+=2){
ans+=a[i]*x,x+=a[i];
if(i>=t+t)x-=a[i-t-t];
}
for(int i=1,x=0;i<n;i+=2){
ans+=a[i]*x,x+=a[i];
if(i>=t+t)x-=a[i-t-t];
}
// debug(ans);
for(int i=1;i<=t;i++){
ans+=max(0,n-(i*2+1)+1);
}
// debug(ary(a,0,n-1),ans);
return ans;
}
void get(){
scanf("%lld%d",&p,&t);
n=min(p-1,2ll*t);
ll res=1ll*t*calc(p),ans=0;
for(int i=1;i<=n;i++)a[i]=chk(i,p)?1:-1;
for(int i=1;i<=n;i++)b[i]=chk(p-i,p)?1:-1;
for(int i=1;i<=n;i++)c[n-i+1]=b[i];
c[n+1]=0;
for(int i=1;i<=n;i++)c[n+1+i]=a[i];
ans=solve(c+1,n*2+1)-solve(a,n+1)-solve(b,n+1);
for(int i=1;i<=n&&i<=t;i++){
ans-=a[i]*b[i]+1;
}
// debug(res,ans);
printf("%lld\n",res-ans/4);
}
int main(){
for(scanf("%d",&T);T--;)get();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1262ms
memory: 50356kb
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: 247ms
memory: 9844kb
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: 0
Accepted
time: 199ms
memory: 3716kb
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:
ok 500000 numbers
Test #4:
score: 0
Accepted
time: 1280ms
memory: 3712kb
input:
500000 796985057191 1 567957900049 1 827610855103 1 919580142883 1 705957627181 1 854537570797 1 653636687779 1 516224177893 1 616020013397 1 518523794483 1 904311938423 1 534402190409 1 578460069899 1 754072383349 1 556038395257 1 676067642057 1 759949674839 1 913727773951 1 792376257731 1 51332043...
output:
199246264296 141989475010 206902713774 229895035720 176489406794 213634392698 163409171944 129056044472 154005003348 129630948620 226077984604 133600547600 144615017474 188518095836 139009598812 169016910512 189987418708 228431943486 198094064432 128330108024 213838430616 148581110810 138371413484 1...
result:
ok 500000 numbers
Test #5:
score: 0
Accepted
time: 1476ms
memory: 3824kb
input:
500000 4630931 1 532378711517 2 897492424691 1 933429720763 2 50396453 1 965938979207 1 757130529707 1 585648391093 2 93206693 1 582531600551 1 902156055859 1 673348731047 1 43627163 2 786341452943 1 630094950481 2 500747394781 2 55003589 2 662875627321 2 554939202737 2 729610579949 1 48444437 2 644...
output:
1157732 266189355756 224373106172 466714860380 12599112 241484744800 189282632426 292824195542 23301672 145632900136 225539013964 168337182760 21813580 196585363234 315047475234 250373697386 27501792 331437813654 277469601364 182402644986 24222216 161182653834 403691568340 409143875612 24208052 1779...
result:
ok 500000 numbers
Test #6:
score: 0
Accepted
time: 1853ms
memory: 3660kb
input:
500000 999999769291 2 999999916099 2 999999337661 2 999999619897 2 999999711979 2 999999125599 2 999999617677 2 999999902791 2 999999704749 2 999999640721 2 999999284629 2 999999430669 2 999999876917 2 999999555559 2 999999545273 2 999999426997 2 999999814963 2 999999272717 2 999999621109 2 99999955...
output:
499999884644 499999958048 499999668828 499999809942 499999855988 499999562796 499999808834 499999951392 499999852370 499999820356 499999642310 499999715330 499999938456 499999777776 499999772632 499999713494 499999907480 499999636356 499999810550 499999775204 499999646592 499999695462 499999792794 4...
result:
ok 500000 numbers
Test #7:
score: -100
Wrong Answer
time: 195ms
memory: 3728kb
input:
182082 73 3 11 5 83 3 71 8 67 1 97 3 11 8 89 4 37 4 23 1 43 7 13 5 41 5 71 9 7 6 41 6 59 4 89 7 29 3 53 8 13 9 53 4 53 7 43 1 67 10 73 10 67 4 31 2 79 4 43 5 11 10 29 2 37 1 17 6 89 2 79 3 47 1 47 1 53 3 43 10 67 1 97 6 83 6 31 2 7 8 89 7 19 6 5 9 17 10 59 2 19 5 61 6 67 7 97 5 89 5 5 3 13 3 83 1 29...
output:
44 6 56 126 16 62 8 76 26 4 58 4 34 142 0 38 54 126 18 82 4 48 74 10 138 128 58 12 68 44 12 12 8 6 40 54 10 10 36 76 16 116 110 12 0 126 18 0 10 28 16 74 100 98 94 0 4 20 40 18 10 4 10 14 32 36 38 94 54 66 20 4 66 6 96 20 62 6 62 12 20 50 58 0 122 50 8 104 20 6 20 0 82 2 26 126 38 0 128 22 116 16 12...
result:
wrong answer 7th numbers differ - expected: '6', found: '8'