QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135697 | #5301. Modulo Ruins the Legend | yuhui | WA | 1ms | 3480kb | C++14 | 2.2kb | 2023-08-05 22:29:12 | 2023-08-05 22:29:15 |
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;
//cout<<t<<" "<<d2<<" "<<A<<"\n";
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 += 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;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3480kb
input:
6 24 1 1 4 5 1 4
output:
1 0 0
result:
wrong answer Result not equal to solution.