https://ac.nowcoder.com/acm/contest/9983/A

思路:
首先特判0,0,0;
其次第一点: x%p==a <--->必存在(p-1)*k1%p==a.所以贪心上是(p-1)

那问题就变成了构造q1,q2互质且他们 (p-1)*q1%p==a&&(p-1)*p2%p==b
然后将k1,k2给转化成式子。
(p-1)*k1%p==a;
(p-1)*k2%p==b;
再将k1,k2写出表达式
k1=t1p-a
k2=t2p-b
题目要使k1,k2互质,不妨构造一个,使k2=p-b;那么(t1-p)%k2==1 <--->t1p+mk2=1+a. 这个p,k2是已知的,t1,m就是系数,就等价ax+by=c;已知a,b,用exgcd求一个特解。
再构造通解

统统回代回去。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL exgcd(LL &x,LL &y,LL a,LL b)
{
if(!b)
{
x=1;
y=0;
return a;
}
LL ret=exgcd(y,x,b,a%b);
y-=a/b*x;
return ret;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL t;cin>>t;
while(t--){
LL a,b,p;cin>>a>>b>>p;
LL k1=p-a,k2=p-b,x,y;
if(!a&&!b) cout<<0<<" "<<0<<" "<<0<<"\n";
else{
exgcd(x,y,p,k2);
x=(x%k2+k2)%k2;
if(!x) x+=k2;
x*=(1+a);
k1=x*p-a;
cout<<(p-1)<<" "<<k1*(p-1)<<" "<<k2*(p-1)<<"\n";
}
}
return 0;
}

博客围绕ACM竞赛题目https://ac.nowcoder.com/acm/contest/9983/A展开,先特判0,0,0,接着利用贪心思路确定为(p - 1),将问题转化为构造互质的q1、q2满足特定条件,把k1、k2写成表达式,通过构造使k2 = p - b,用exgcd求特解并构造通解。

681

被折叠的 条评论
为什么被折叠?



