QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#627549#5301. Modulo Ruins the Legendnahida_qaqRE 0ms3632kbC++171.9kb2024-10-10 16:17:002024-10-10 16:17:03

Judging History

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

  • [2024-10-10 16:17:03]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3632kb
  • [2024-10-10 16:17:00]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define io ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb push_back
using namespace std;
const int N=1e6+5,mod1=1e9+7,mod2=998244353;
typedef pair<int,int> pi;
int a[N];
int exgcd(int a, int b, int& x, int& y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  int d = exgcd(b, a % b, x, y);
  int temp = x;
  x = y;
  y = temp - a / b * y;
  return d;
}
void solve()
{
    int n,m;
    cin>>n>>m;
    int sum=0;
    for(int i=1;i<=n;i++)cin>>a[i],sum+=a[i];
    int s0,d0;
    int d1=exgcd(n,n*(n+1)/2,s0,d0);
    int x0,y0;
    int d2=exgcd(d1,m,x0,y0);
    if(n==1)
    {
        cout<<0<<'\n';
        cout<<m%a[1]<<' '<<0;
        return ;
    }
    if(x0%d2==0)
    {
        cout<<0<<'\n';
        int res=abs(x0*sum/d2);
        int t=n*(n+1)/2/d1;
        //cout<<t<<'\n';
        s0*=res,d0*=res;
        s0=(s0%t+t)%t;
        d0=(d0%t+t)%t;
        cout<<s0<<' '<<d0;
    }
    else
    {
        int w=d2/gcd(x0,d2);
        int num=sum/w;
        int c=min(sum-num*w,num*w+w-sum);
        cout<<c<<'\n';
        int res=(x0*(c-sum)/d2)*d1;
        //cout<<res<<'\n';
        int dd=gcd(res,gcd(n,n*(n+1)/2));
        res/=dd;
        int x1=n/dd,x2=n*(n+1)/2/dd;
        int ss0,dd0;
        int ddd=exgcd(x1,x2,ss0,dd0);
        if(res<0)res=(res%m+m);
        res=res/ddd;
        ss0*=res,dd0*=res;
        if(ss0<0)
        {
            ss0+=abs(ss0)/x2*x2;
            dd0-=abs(ss0)/x2*x1;
            if(ss0<0)ss0+=x2,dd0-=x1;
        }
        if(dd0<0)
        {
            dd0+=abs(dd0)/x1*x1;
            ss0-=abs(dd0)/x1*x2;
            if(dd0<0)dd0+=x1,ss0-=x2;
        }
        cout<<ss0<<' '<<dd0;
    }
}
signed main()
{
    io;
    int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

input:

6 24
1 1 4 5 1 4

output:

1
6 17

result:

ok ok

Test #2:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

7 29
1 9 1 9 8 1 0

output:

0
0 0

result:

ok ok

Test #3:

score: -100
Runtime Error

input:

1 1
0

output:


result: