QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135695 | #5301. Modulo Ruins the Legend | yuhui | WA | 9ms | 3852kb | C++14 | 2.1kb | 2023-08-05 22:21:28 | 2023-08-05 22:21:32 |
Judging History
answer
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int a[MAXN]={};
ll S = 0;
ll gcd(ll a,ll b)
{
return b ? gcd(b,a%b) : a;
}
ll gcd(ll a,ll b,ll c)
{
return gcd(a,gcd(b,c));
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
ll d = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return d;
}
// S = S%m N = n*(n+1)/2
// (S + n*s + N * d ) %m = ans (ans-S) is a multiply of gcd(m,n,n*(n+1)/2)
// gcd(n,N)*t - my = ans - S (A = n*s + n*(n+1)/2 * d = gcd(n,N)*t)
// gcd(n,N)*t - my = ans - S = k*gcd(n,N,m);
// ans = S + k*d
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n = 0,m = 0;
cin>>n>>m;
ll N = n*(n+1)/2;
for(int i=1;i<=n;++i){
cin>>a[i];
S += a[i];
}
//cin>>S;
S%=m;
if(n == 1 && S == 0){
cout<<"0\n0 0\n";
return 0;
}
ll d1 = gcd(n,N,m);
ll d2 = gcd(n,N);
ll k = S/d1;
ll ans = S - k*d1;
cout<<ans<<"\n";
ll t = 0, y = 0;
exgcd(d2,m,t,y);
t = -t*k, y = -y*k;
//t += 100*m/d1,y-=100*d2/d1;
//cout<<t <<" "<<y <<" "<<t*d2 + m*y<<" "<<ans - S<<"\n";
ll s = 0, d = 0;
ll div = t/(m/d1);
t -= div*m/d1;
while(1){
ll A = t*d2;
exgcd(n,N,s,d);
s = s*A/d2, d = d*A/d2;
if(s < 0){
ll mul = -s/(N/d2) + 1;
s += mul*N/d2;
d -= mul*n/d2;
}
if(d < 0){
ll mul = -d/(n/d2) + 1;
d += mul*n/d2;
s -= mul*N/d2;
}
if(s >= m){
ll mul = (s-m)/(N/d2) + 1;
d += mul*n/d2;
s -= mul*N/d2;
}
//cout<<s<<" "<<d<<" "<<t<<"\n";
t += (n>1?(n-1):1)*m/d1;
if( s<0 || d<0 || s>m || d>m){continue;}
cout<<s<<" "<<d<<"\n";
//cout<<n*s + n*(n+1)/2 *d <<" "<<A<<"\n";
//cout<< A<<" "<<y<<" "<<A - y*m <<" "<< ans - S<<"\n";
//cout<< (S + n*s + N%m * d)%m<<"\n";
break;
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3416kb
input:
6 24 1 1 4 5 1 4
output:
1 7 3
result:
ok ok
Test #2:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
7 29 1 9 1 9 8 1 0
output:
0 0 0
result:
ok ok
Test #3:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
1 1 0
output:
0 0 0
result:
ok ok
Test #4:
score: 0
Accepted
time: 1ms
memory: 3464kb
input:
1 1000000000 963837005
output:
0 0 36162995
result:
ok ok
Test #5:
score: 0
Accepted
time: 1ms
memory: 3464kb
input:
2 1 0 0
output:
0 0 0
result:
ok ok
Test #6:
score: 0
Accepted
time: 1ms
memory: 3476kb
input:
2 1000000000 948507269 461613424
output:
0 1 196626435
result:
ok ok
Test #7:
score: 0
Accepted
time: 4ms
memory: 3852kb
input:
100000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
0 0 0
result:
ok ok
Test #8:
score: 0
Accepted
time: 7ms
memory: 3852kb
input:
100000 1000000000 253614966 278270960 980235895 498158918 928430170 216003119 852570558 948400590 239257296 897053667 294741176 38297441 382677590 406314557 609468973 854148232 314532767 738191551 158215002 5865825 920471826 380037058 356271728 749175327 28319049 208101105 953758995 896570758 521930...
output:
46613 24641 19999
result:
ok ok
Test #9:
score: -100
Wrong Answer
time: 9ms
memory: 3808kb
input:
100000 998244353 561002596 498658036 721339539 63377827 532179242 934651519 234198881 490149304 2056307 499913682 427679408 694677560 516580968 300129454 816286800 688594301 183049581 456627420 495848352 273497462 953217060 796225499 207179832 728054671 409492082 25003432 810431468 206421553 5569626...
output:
0 84628 13480
result:
wrong answer Result not equal to solution.