1038 X^A Mod P
基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题
X^A mod P = B,其中P为质数。给出P和A B,求< P的所有X。
例如:P = 11,A = 3,B = 5。
3^3 Mod 11 = 5
所有数据中,解的数量不超过Sqrt(P)。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 100)
第2 - T + 1行:每行3个数P A B,中间用空格隔开。(1 <= A, B < P <= 10^9, P为质数)
Output
共T行,每行包括符合条件的X,且0 <= X < P,如果有多个,按照升序排列,中间用空格隔开。如果没有符合条件的X,输出:No Solution。所有数据中,解的数量不超过Sqrt(P)。
Input示例
3
11 3 5
13 3 1
13 2 2
Output示例
3
1 3 9
No Solution
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug puts("===============")
using namespace std;
typedef long long ll;
//快速幂
ll pow_mod(ll a, ll n, ll m) {
ll res = 1;
while(n) {
if (n & 1) res = res * a % m;
n >>= 1;
a = a * a % m;
}
return res;
}
//求原根
vector<ll> a;
bool g_test(ll g, ll p) {
for (ll i = 0; i < a.size(); i++)
if (pow_mod(g, (p - 1) / a[i], p) == 1) return 0;
return 1;
}
ll primitive_root(ll p) {
a.clear();
ll tmp = p - 1;
for (ll i = 2; i <= tmp / i; i++) if (tmp % i == 0) { //这里还可以用筛素数优化
a.push_back(i);
while(tmp % i == 0) tmp /= i;
}
if (tmp != 1) a.push_back(tmp);
ll g = 1;
while(true) {
if (g_test(g, p)) return g;
g++;
}
}
// 求离散对数
#define N 111111
struct node {
ll x, id;
bool operator < (const node & T) const {
if (x == T.x) return id < T.id;
return x < T.x;
}
}E[N];
ll discrete_log(ll x, ll n, ll m) {
int s = sqrt(m + 0.5);
for (; (ll) s * s <= m; ) s++;
ll cur = 1;
node tmp;
for (int i = 0; i < s; i++) {
tmp.id = i, tmp.x = cur;
E[i] = tmp;
cur = cur * x % m;
}
sort(E, E + s);
ll mul = pow_mod(cur, m - 2, m) % m; // mul = 1 / (x^s)
cur = 1;
for (int i = 0; i < s; i++) {
ll more = (ll) n * cur % m;
tmp.id = -1, tmp.x = more;
int pos = lower_bound(E, E + s, tmp) - E;
if (E[pos].x == more) return i * s + E[pos].id;
cur = cur * mul % m;
}
return -1;
}
//扩展欧几里得
ll extend_gcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
else {
ll r = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
}
//N次剩余
//给定n,a,p 求出x^n ≡ a(mod p)在模p意义下的所有解,其中p是素数
vector<ll> residue(ll p, ll n, ll a) {
vector<ll> ret;
if (a == 0) {
ret.push_back(0);
return ret;
}
ll g = primitive_root(p);
ll m = discrete_log(g, a, p);
if (m == -1) return ret;
ll A = n, B = p - 1, C = m, x, y;
ll d = extend_gcd(A, B, x, y);
if (C % d != 0) return ret;
x = x * (C / d) % B;
ll delta = B / d;
for (int i = 0; i < d; i++) {
x = ((x + delta) % B + B) % B;
ret.push_back(pow_mod(g, x, p));
}
sort(ret.begin(), ret.end());
ret.erase(unique(ret.begin(), ret.end()), ret.end());
return ret;
}
int main () {
ll n, a, p;
int T;
scanf("%d",&T);
for(int i=0;i<T;i++)
{
scanf("%lld%lld%lld", &p, &n, &a);
vector<ll> ret = residue(p, n, a);
//printf("%d\n", ret.size());
for (int i = 0; i < ret.size(); i++) printf("%d ", ret[i]);
if(ret.size()>0)
printf("\n");
if(ret.size()==0)
printf("No Solution\n");
}
直接粘代码吧,知识点太多了。