给定\(p, k, A\),满足\(k, p\)是质数,求

\[x^k \equiv A \mod p\]

不会。。。

upd:3:29

两边取指标,是求

\[k\text{ind}_x\equiv \text{ind}_A\mod p-1\]

的解数,先求最小的解,然后暴力求之后的就行了。

 #include <iostream>
#include <map>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long LL; vector<LL> f, as;
LL fast_pow(LL base, LL index, LL mod) {
LL ret = ;
for(; index; index >>= , base = base * base % mod)
if(index & ) ret = ret * base % mod;
return ret;
}
bool test_Primitive_Root(LL g, LL p) {
for(LL i = ; i < f.size(); ++i)
if(fast_pow(g, (p - ) / f[i], p) == )
return ;
return ;
}
LL get_Primitive_Root(LL p) {
f.clear();
LL tmp = p - ;
for(LL i = ; i <= tmp / i; ++i)
if(tmp % i == )
for(f.push_back(i); tmp % i == ; tmp /= i);
if(tmp != ) f.push_back(tmp);
for(LL g = ; ; ++g) {
if(test_Primitive_Root(g, p))
return g;
}
}
LL get_Discrete_Logarithm(LL x, LL n, LL m) {
map<LL, int> rec;
LL s = (LL)(sqrt((double)m) + 0.5), cur = ;
for(LL i = ; i < s; rec[cur] = i, cur = cur * x % m, ++i);
LL mul = cur;
cur = ;
for(LL i = ; i < s; ++i) {
LL more = n * fast_pow(cur, m - , m) % m;
if(rec.count(more))
return i * s + rec[more];
cur = cur * mul % m;
}
return -;
}
LL ext_Euclid(LL a, LL b, LL &x, LL &y) {
if(b == ) {
x = , y = ;
return a;
} else {
LL ret = ext_Euclid(b, a % b, y, x);
y -= x * (a / b);
return ret;
}
}
void solve_Linear_Mod_Equation(LL a, LL b, LL n) {
LL x, y, d;
as.clear();
d = ext_Euclid(a, n, x, y);
if(b % d == ) {
x %= n, x += n, x %= n;
as.push_back(x * (b / d) % (n / d));
for(LL i = ; i < d; ++i)
as.push_back((as[] + i * n / d) % n);
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin); freopen("data.out", "w", stdout);
#endif LL p, k, a;
cin >> p >> k >> a;
if(a == ) {
puts("1\n0");
return ;
}
LL g = get_Primitive_Root(p);
LL q = get_Discrete_Logarithm(g, a, p);
solve_Linear_Mod_Equation(k, q, p - );
for(int i = ; i < as.size(); ++i)
as[i] = fast_pow(g, as[i], p);
sort(as.begin(), as.end());
printf("%d\n", as.size());
for(int i = ; i < as.size(); ++i) {
printf("%lld%c", as[i], i == as.size() - ? '\n' : ' ');
}
return ;
}

SGU 261. Discrete Roots的更多相关文章

  1. SGU 261. Discrete Roots (N次剩余)

    N次剩余 题目:http://acm.sgu.ru/problem.php? contest=0&problem=261 题意:给定n,a,p 求出x^n ≡ a(mod p)在模p意义下的全 ...

  2. sgu 261

    学习了元根的一些知识,哈哈. 总结一下: 几个概念: 阶:对于模数m和整数a,并且gcd(m,a)==1,那么定义a在模m下的阶r为满足ar=1 mod m的最小正整数. 性质1:r in [1,ph ...

  3. 一些数论概念与算法——从SGU261谈起

    话说好久没来博客上面写过东西了,之前集训过于辛苦了,但有很大的收获,我觉得有必要把它们拿出来总结分享.之前一直是个数论渣(小学初中没好好念过竞赛的缘故吧),经过一道题目对一些基础算法有了比较深刻的理解 ...

  4. UVA 1426 - Discrete Square Roots(数论)

    UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...

  5. UVa 1426 Discrete Square Roots (扩展欧几里德)

    题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...

  6. Discrete Square Roots UVALive - 4270(拓展欧几里得)

    a≡b(mod n)的含义是“a和b除以n的余数相同”,其充要条件是“a-b是n的整数倍”: 求所有满足条件r^2=x(mod m)的r 题目已经给定了一个初始的r,x,m #include < ...

  7. UVALive 4270 Discrete Square Roots

    题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...

  8. UVALive - 4270 Discrete Square Roots (扩展欧几里得)

    给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...

  9. UVA1426 Discrete Square Roots

    思路:\(exgcd\) 提交:\(2\)次 错因:输出格式错误OTZ 题解: 求:\(r^2 ≡ x \mod N , 0 \leq r < N\),并且题目会给出 \(x,N\) 和一个合法 ...

随机推荐

  1. bzoj 1189 [HNOI2007]紧急疏散evacuate 二分+网络流

    [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3626  Solved: 1059[Submit][St ...

  2. DOM基本代码二

    ------------------------------- <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh ...

  3. Sublime Text 配置python文件

    每次配置Sublime Text的都是好一顿搜索配置信息,今天特地把自己电脑上配置好的信息保存下来,方便以后使用. 用到了 AutoPEP8.Anaconda.SublimeCodeIntel.Sub ...

  4. Android核心类源码分析

    Handler流程1.首先Looper.prepare()在本线程中保存一个Looper实例,然后该实例中保存一个MessageQueue对象:因为Looper.prepare()在一个线程中只能调用 ...

  5. JavaScript知识之判断字符串中出现最多的字符及次数

    var str = 'asdddasdfdseeeeeweeeeeeeeeeeee'; var json = {}; // 定义json一个对象 for(var i = 0; i < str.l ...

  6. 图论&数学:矩阵树定理

    运用矩阵树定理进行生成树计数 给定一个n个点m条边的无向图,问生成树有多少种可能 直接套用矩阵树定理计算即可 矩阵树定理的描述如下: 首先读入无向图的邻接矩阵,u-v G[u][v]++ G[v][u ...

  7. vs 自定义插件(扩展工具)

    此篇仅仅是因为好奇,实现的是完全没有价值的东西,当然,通过此篇的尝试,后续可以在适当的场景,深入的研究Visual Studio自定义插件的应用. 实现功能如下: 在鼠标选中的地方,显示一下创建人,创 ...

  8. SSM三大框架整合详细总结(Spring+SpringMVC+MyBatis)(山东数漫江湖)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经很久了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录 ...

  9. 【洛谷 P2762】 太空飞行计划问题(最大权闭合图)

    题目链接 最大权闭合图模型,参考 具体做法是从源点向每个实验连一条流量为这个实验的报酬的边,从每个实验向这个实验需要的所有器材各连一条流量为\(INF\)的边,再从每个器材向汇点连一条流量为这个器材的 ...

  10. javascript延迟对象

    1.模拟任务队列: function taskQueue() { var taskList = []; var isRun = false; this.addTask = function (task ...