首页 > 代码库 > 【UVA】12169-Disgruntled Judge(暴力or欧几里得)

【UVA】12169-Disgruntled Judge(暴力or欧几里得)

可能由于后台数据的原因,这道题直接暴力枚举a,b进行判断也能过,不过跑的时间长,效率太差了。

1402100612169Disgruntled JudgeAcceptedC++0.8762014-08-11 08:46:28

不说了,比较无脑。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF 1 << 10
#define esp 1e-6
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 200 + 10
int main(){
    int T;
    int x[MAXD];
    scanf("%d",&T);
    for(int i = 1 ; i < 2 * T ; i+= 2)
        scanf("%d",&x[i]);
    int ok,a,b;
    for(a = 0 ; a <= 10000 ; a++){
        for(b = 0; b <= 10000 ; b++){
             ok = 1;
             for(int i = 2 ; i <= 2 * T ; i++){
                 if(i & 1){
                    if(x[i] != ((a * x[i - 1] + b) % 10001)){
                        ok = 0;
                        break;
                    }
                 }
                 else{
                    x[i] = (a * x[i - 1] + b) % 10001;
                 }
             }
             if(ok)
                break;
        }
        if(ok)
            break;
    }
    for(int i = 2 ; i <= 2 * T ; i+= 2)
    printf("%d\n",x[i]);
    return 0;
}

再一个办法就是枚举a,利用x1,x3求出b,判断所有x的关系能不能满足a,b。

如何通过a,x1,x3求出b呢。

x2 = (a * x1 + b) % 10001;

x= (a * x2 + b) % 10001;

联立2个式子

x3 = (a * (a * x1 + b) % 10001 + b ) % 10001;

x3 = (a * (a * x1 + b) + b) % 10001;

所以 x3 + 10001 * k = a * a * x1 + (a + 1) * b;

x3 - a * a * x1 = (a + 1) * b + 10001 * (-k);

这样就成了求 b 和 -k,满足这个式子,不就是扩展欧几里得的一般用法么?

1402148412169Disgruntled JudgeAcceptedC++0.0122014-08-11 10:54:22

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<string>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF 1 << 10
#define esp 1e-6
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 200 + 10
#define max_size 10001
void gcd(LL a , LL b ,LL &d, LL &x,LL &y){
    if(!b){
        d = a ;
        x = 1;  y = 0;
        return ;
    }
    else{
        gcd(b , a % b ,d , y , x);
        y -= x * (a / b);
        return ;
    }
}
int main(){
    LL a,b,x[MAXD];
    int T;
    scanf("%d",&T);
    for(int i = 1 ; i < 2 * T  ; i += 2)
        scanf("%lld",&x[i]);
    for(a = 0; ; a++){
        LL k , b , d;
        LL t = (x[3] - a * a * x[1]);
        gcd(max_size, a + 1, d , k, b);
        if(t % d) continue;
        b = b * t / d;
        int yes = 1;
        for(int i = 2 ; i <= 2 * T ; i ++){
            if(i & 1){
                if(x[i] != ((a * x[i - 1] + b) % max_size)){
                     yes = 0;
                     break;
                }
            }
            else{
                x[i] = (a * x[i - 1] + b) % max_size;
            }
        }
        if(yes)
            break;
    }
    for(int i = 2 ; i <= 2 * T ; i+=2)
        printf("%lld\n",x[i]);
    return 0;
}