UVA - 12169 扩展GCD

  • 题意:

    • 数列x[2*n]是按照xi=(axi1+b)%10001生成的,现在给你n个奇数项 x1,x3,x5x2n1,求偶数项,(一组可行解)
  • 规模:

    • 1<=n,xi<=10000
    • 1<=a,b<=10000
    • 1<=T<=100
  • 类型:

    • 数论,gcd
  • 分析:

    • x2=ax1+bmody
    • x3=ax2+bmody
    • 1式代入2式得:x3a2x1=(a+1)b+mody
    • 枚举a,通过ex_gcd得b,判断是否满足
  • 时间复杂度&&优化:

    • 看上去像n2,但实际ex_gcd这里只有(x3a2x1)%gcd(a+1,mod)==0时b有解,实际很少。
  • 代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>


using namespace std;

const int INF=0x3fff3fff;
const int MAXN=1010;
const int MAXM=40010;
const int MOD=10001;
typedef long long ll;

//****************
//返回d=gcd(a,b);和对应于等式ax+by=d中的x,y
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
    if(a==0&&b==0)return -1;
    if(b==0){x=1;y=0;return a;}
    long long d=extend_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

int n,m;
long long x[2100];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    while(cin>>n){
        n*=2;
        for(int i=1;i<=n;i+=2){
            cin>>x[i];
        }
        long long a,b,y;
        for(a=0;;a++){
            long long c=x[3]-a*a*x[1];
            long long d=extend_gcd(a+1,MOD,b,y);
            if(c%d)continue;
            b=b*c/d;
            //cout<<a<<endl;
            int flag=1;
            int i;
            for(i=2;i<=n;i++){
                if(i&1){
                    if(x[i]!=(a*x[i-1]+b)%MOD){break;}
                }
                else x[i]=(a*x[i-1]+b)%MOD;
               // cout<<i<<" "<<x[i]<<endl;
            }
            if(i>n)break;
        }
        for(int i=2;i<=n;i+=2)
            printf("%lld\n",x[i]);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值