QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#586881 | #5301. Modulo Ruins the Legend | wsxcb# | WA | 38ms | 3696kb | C++17 | 1017b | 2024-09-24 16:21:54 | 2024-09-24 16:21:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll INF=1e18;
ll exgcd(ll a,ll b ,ll &x, ll &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
ll ans=exgcd(b,a%b,x,y);
ll x0=x,y0=y;
x=y0,y-=x0-a/b*y0;
return ans;
}
void solve(){
int n,m;
cin>>n>>m;
ll sum=0;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
sum+=x;
}
ll k=1ll*n*(n+1)/2;
ll g=__gcd(n,m);
ll ans=INF,pp,ansd;
for(int i=0;i<=500000;i++)
{
ll s=sum+1ll*i*k;
s%=m;
ll s1=s;
s=m-s;
ll l=0,r=1e9,pos;
while(l<=r)
{
ll mid=(l+r)/2;
if(mid*g>=s){
r=mid-1;
pos=mid;
}
else
l=mid+1;
}
s1=(s1+g*pos)%m;
if(s1<ans)
{
ans=s1;
ansd=i;
pp=pos*g;
}
}
cout<<ans<<'\n';
ll x,y;
ll d=exgcd(n,m,x,y);
ll cnt=pp/d;
x*=cnt;
ll res=abs(m/d);
cout<<(x%res+res)%res<<' ';
cout<<ansd;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 23ms
memory: 3628kb
input:
6 24 1 1 4 5 1 4
output:
1 2 1
result:
ok ok
Test #2:
score: 0
Accepted
time: 23ms
memory: 3576kb
input:
7 29 1 9 1 9 8 1 0
output:
0 0 0
result:
ok ok
Test #3:
score: 0
Accepted
time: 23ms
memory: 3648kb
input:
1 1 0
output:
0 0 0
result:
ok ok
Test #4:
score: -100
Wrong Answer
time: 38ms
memory: 3696kb
input:
1 1000000000 963837005
output:
0 963837005 0
result:
wrong answer Result not equal to solution.