POJ3243 Clever Y【高次同余方程】

题目链接:

http://poj.org/problem?id=3243

题目大意:

已知公式A^x mod C= B,以及A、C、B的值,求解x的值为多少。

思路:

典型的求解方程A^x = B(mod C),直接模板解决。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL __int64
using namespace std;
const int MAXN = 65535;

struct HASH
{
    int a;
    int b;
    int next;
}Hash[MAXN*2];

int flag[MAXN+66];
int top,idx;

void ins(int a,int b)
{
    int k = b & MAXN;
    if(flag[k] != idx)
    {
        flag[k] = idx;
        Hash[k].next = -1;
        Hash[k].a = a;
        Hash[k].b = b;
        return;
    }

    while(Hash[k].next != -1)
    {
        if(Hash[k].b == b)
            return;
        k = Hash[k].next;
    }
    Hash[k].next = ++top;
    Hash[top].next = -1;
    Hash[top].a = a;
    Hash[top].b = b;
}

int Find(int b)
{
    int k = b & MAXN;
    if(flag[k] != idx)
        return -1;
    while(k != -1)
    {
        if(Hash[k].b == b)
            return Hash[k].a;
        k = Hash[k].next;
    }
    return -1;
}

int GCD(int a,int b)
{
    if(b == 0)
        return a;
    return GCD(b,a%b);
}

int ExGCD(int a,int b,int &x,int &y)
{
    int temp,ret;
    if(!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    ret = ExGCD(b,a%b,x,y);
    temp = x;
    x = y;
    y = temp - a/b*y;
    return ret;
}

int Inval(int a,int b,int n)
{
    int x,y,e;
    ExGCD(a,n,x,y);
    e = (LL)x*b%n;
    return e < 0 ? e + n : e;
}

int PowMod(LL a,int b,int c)
{
    LL ret = 1%c;
    a %= c;
    while(b)
    {
        if(b&1)
            ret = ret*a%c;
        a = a*a%c;
        b >>= 1;
    }
    return ret;
}

int BabyStep(int A,int B,int C)
{
    top = MAXN;
    ++idx;
    LL buf = 1%C,D = buf,K;
    int d = 0,temp,i;
    for(i = 0; i <= 100; buf = buf*A%C,++i)
    {
        if(buf == B)
            return i;
    }

    while((temp = GCD(A,C)) != 1)
    {
        if(B % temp)
            return -1;
        ++d;
        C /= temp;
        B /= temp;
        D = D*A/temp%C;
    }

    int M = (int)ceil(sqrt((double)C));
    for(buf = 1%C,i = 0; i <= M; buf = buf*A%C,++i)
        ins(i,buf);

    for(i = 0,K = PowMod((LL)A,M,C); i <= M; D = D*K%C,++i)
    {
        temp = Inval((int)D,B,C);
        int w;
        if(temp >= 0 && (w = Find(temp)) != -1)
            return i * M + w + d;
    }
    return -1;
}

int main()
{
    int A,B,C;
    while(~scanf("%d%d%d",&A,&C,&B) && (A||B||C))
    {
        B %= C;
        int temp = BabyStep(A,B,C);
        if(temp < 0)
            printf("No Solution\n");
        else
            printf("%d\n",temp);
    }

    return 0;
}
时间: 2024-11-09 01:54:59

POJ3243 Clever Y【高次同余方程】的相关文章

【数论】【ex-BSGS】poj3243 Clever Y

用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题的坑点请去看discuss. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; vo

数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSGS算法中是要求a^m在%c条件下的逆元的,如果a.c不互质根本就没有逆元.) 如果x有解,那么0<=x<C,为什么? 我们可以回忆一下欧拉定理: 对于c是素数的情况,φ(c)=c-1 那么既然我们知道a^0=1,a^φ(c)=1(在%c的条件下).那么0~φ(c)必定是一个循环节(不一定是最小的)

POJ 3243 Clever Y BSGS

Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6861   Accepted: 1676 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, give

高次同余方程模板BabyStep-GiantStep

/************************************* ---高次同余方程模板BabyStep-GiantStep--- 输入:对于方程A^x=B(mod C),调用BabyStep(A,B,C),(0<=A,B,C<=10^9) 输出:无解放回-1,有解放回最小非负整数x 复杂度:O(C^0.5),只与C有关,与A,B的大小无关 ************************************/ typedef long long ll; #define HAS

POJ2417 Discrete Logging【高次同余方程】

题目链接: http://poj.org/problem?id=2417 题目大意: 已知整数P.B.N满足公式B^i = N(mod P),求i的值是多少. 思路: 典型的解高次同余方程A^x = B(mod C),直接套模板解决.注意输入顺序:C A B AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath>

【EXT-BSGS算法求离散对数】POJ Clever Y 3243

Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7259 Accepted: 1795 Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, Y, Z, we all know how to figure out K fast. However, given X,

【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS

[BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input 每个测试文件中最多包含100组测试数据. 每组数据中,每行包含3个正整数a,p,b. 当a=p=b=0时,表示测试数据读入完全. Output 对于每组数据,输出一行. 如果无解,输出“No Solution”(不含引号),否则输出最小自然数解. Sample Input 5 58 33 2 4 3 0 0

poj 3243 Clever Y 高次方程

1 Accepted 8508K 579MS C++ 2237B/** 2 hash的强大,,还是高次方程,不过要求n不一定是素数 3 **/ 4 #include <iostream> 5 #include <cstdio> 6 #include <cmath> 7 #include <cstring> 8 #include <algorithm> 9 using namespace std; 10 long long a,b,n; 11 co

Clever Y POJ - 3243 (扩展BSGS)

Clever Y POJ - 3243 题意:给a,c,b,求最小的x使得 ax≡b (mod c). 扩展BSGS算法~ 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #define ll long long 6 using namespace std; 7 const int mod=99991; 8 ll head[mod],nex