1. 程式人生 > >【UVA】12169-Disgruntled Judge(暴力or歐幾里得)

【UVA】12169-Disgruntled Judge(暴力or歐幾里得)

可能由於後臺資料的原因,這道題直接暴力列舉a,b進行判斷也能過,不過跑的時間長,效率太差了。

14021006 12169 Accepted C++ 0.876 2014-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,滿足這個式子,不就是擴充套件歐幾里得的一般用法麼?

14021484 12169 Accepted C++ 0.012 2014-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;
}