UVA 12169 - Disgruntled Judge(扩展欧几里得,等式求解)

这个题在求a,b的时候。 需要两个等式。 写出 x3 x2 的关系式  和 x2 x1的关系式。 联立之后。


便可以写成 (a + 1)b + 10001 y = x3 - a*a*x1;  


这个式子 明显的就是  在模拟 a的情况下。  a+1 10001为系数   已知,在知道  c = x3 - a*a*x1  则求 b,y。


在刘汝佳第二版。P314最上面 写的很清楚。 ax+by=c 的一组解是  (x0c/g , y0c/g)。   其中 x0 y0 g 都可以 在扩展欧几里得里面求出。


在这一组解中 求出的 x y是任意一组。 所以计算就好。 


如果想把 b 求导最小。 在 P313 提示10-1 中 写的很明白了。 求出 b‘ = b/d; b(min) = x0 + b’  (原式为 x0+kb’ ,为了取最小 令k  = 1)


在求出 a,b之后 进行求解数列, 注意 如果得出的某一奇数项 与给出的不相同。 那么 a,b不成立, 需要重新枚举a。


#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 1000+10
#define INF 1<<30
ll 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 n;
    scanf("%d",&n);
    ll s[maxn] = {0};
    for(int i = 1; i <= n; i++){
        scanf("%lld",&s[2*i - 1]);
    }
    for(int i = 0;i <= 10000; i++){
        int flag = 1;
        ll a = i;
        ll b,k,d;
        gcd(a+1,10001,d,b,k);
        ll c = (s[3] - s[1]*a*a)%10001;
        if(c % d) continue; // 无整数解
        b = b * c / d % 10001;   // 求出一组解
        ll x[maxn];
        memcpy(x,s,sizeof(s));

        for(int j = 1; j < 2 * n; j++){
            int xj = (a * x[j] + b)%10001;
            if(j % 2 == 1)
                x[j+1] = xj;
            else if(xj != x[j+1]){
                flag = 0;
                break;
            }
        }
        if(flag){
            for(int j = 2; j <= 2*n ; j+=2){
                printf("%d\n",x[j]);
            }
            break;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值