uva 12169 扩展Gcd

题意:

给定公式xi=(xi-1*a+b)%10001
输入x1,x2,x3…..x2T-1,输出x2,x4,x6……x2T

思路:

枚举a[0,10000],通过扩展gcd计算出b

X2=(X1*a+b)%10001;
X3=(a*(X1*a+b)%10001+b)%10001;
X3=(a*(X1*a+b)+b)%10001;
X3+10001y=a*(X1*a+b)+b=X1*a*a+a*b+b
X3-X1*a*a=10001y-(1+a)b

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1000;
LL d,x,y;
LL data[maxn];
const int MOD = 10001;
void gcd(LL a,LL b,LL& d,LL& x,LL& y)
{
    if(!b) {d=a;x=1;y=0;}
    else {gcd(b,a%b,d,y,x);y-=x*(a/b);}
}

int main()
{
      int T;
      cin>>T;
      for(int i=1;i<=2*T;i+=2)
      {
          cin>>data[i];
      }
      for(int a=0;a<=10000;a++)
      {
         LL t = data[3]-a*a*data[1];
         gcd(MOD,a+1,d,x,y);
         if(t%d) continue;
         y=y/d*t;
         bool flag=1;
         for(int i=2;i<=2*T;i++)
         {
             LL tmp = (a*data[i-1]+y)%MOD;
             if(i&1)
             {
                if(data[i]!=tmp)
                {
                    flag=0;
                    break;
                }
             }
             else
             {
                data[i]=tmp;
             }
         }
         if(flag) {break;}
    }
    for(int i=2;i<=2*T;i+=2)
      cout<<data[i]<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值