SGU 261. Discrete Roots
给定\(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的更多相关文章
- 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意义下的全 ...
- sgu 261
学习了元根的一些知识,哈哈. 总结一下: 几个概念: 阶:对于模数m和整数a,并且gcd(m,a)==1,那么定义a在模m下的阶r为满足ar=1 mod m的最小正整数. 性质1:r in [1,ph ...
- 一些数论概念与算法——从SGU261谈起
话说好久没来博客上面写过东西了,之前集训过于辛苦了,但有很大的收获,我觉得有必要把它们拿出来总结分享.之前一直是个数论渣(小学初中没好好念过竞赛的缘故吧),经过一道题目对一些基础算法有了比较深刻的理解 ...
- UVA 1426 - Discrete Square Roots(数论)
UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N. R.要求r2≡x (mod n) (1 <= r < n)的全部解.R为一个已知解 思路: ...
- UVa 1426 Discrete Square Roots (扩展欧几里德)
题意:给定 x,n,r,满足 r2 ≡ x mod(n) ,求在 0 ~ n 内满足 rr2 ≡ x mod(n) 的所有的 rr. 析:很明显直接是肯定不行了,复杂度太高了. r2 ≡ x mod( ...
- 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 < ...
- UVALive 4270 Discrete Square Roots
题目描述: 在已知一个离散平方根的情况下,按照从小到大的顺序输出其他所有的离散平方根. 在模n意义下,非负整数x的离散平方根是满足0<=r<n且r2=x(mod n)的整数r. 解题思路: ...
- UVALive - 4270 Discrete Square Roots (扩展欧几里得)
给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...
- UVA1426 Discrete Square Roots
思路:\(exgcd\) 提交:\(2\)次 错因:输出格式错误OTZ 题解: 求:\(r^2 ≡ x \mod N , 0 \leq r < N\),并且题目会给出 \(x,N\) 和一个合法 ...
随机推荐
- Educational Codeforces Round 6 A
A. Professor GukiZ's Robot time limit per test 0.5 seconds memory limit per test 256 megabytes input ...
- linux和windows多线程的异同
linux多线程及线程同步和windows的多线程之间的异同 并不是所有的程序都必须采用多线程,有时候采用多线程性能还不如单线程.采用多线程的好处如下: (1)多线程之间采用相同的地址空间,共享大部分 ...
- generatorConfiguration配置文件及其详细解读
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...
- Markdown 代码块中再内嵌一个行内代码
在 jQuery 1.9 之前(不含1.9):如果传入一个空字符串. null 或 jQuery.parseJSON( jsonString ) ,该函数将返回,而不是抛出一个错误,即使它不是有效的 ...
- bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分
Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...
- [BZOJ1177][BZOJ1178][BZOJ1179]APIO2009解题报告
抱着好奇心态去开始做APIO的往年试题感受一下难度 Oil Description 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地 ...
- bzoj 2434 fail tree+dfs序
首先比较明显的是我们可以将字符串组建立ac自动机,那么对于询问s1字符串在s2字符串中出现的次数,就是在以s1结尾为根的fail tree中,子树有多少个节点是s2的节点,这样我们处理fail tre ...
- Java 中的成员内部类
内部类中最常见的就是成员内部类,也称为普通内部类.我们来看如下代码: 运行结果为: 从上面的代码中我们可以看到,成员内部类的使用方法: 1. Inner 类定义在 Outer 类的内部,相当于 Out ...
- k8s取节点内docker中的日志
Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部:Borg).在Docker技术的基础上,为容器化的应用提供部署运行.资源调度.服务发现和动态伸缩等一系列完整功能,提高了大 ...
- Python【模块】importlib,requests
内容概要: 模仿django中间件的加载方式 importlib模块 requests模块 rsplit() 用实际使用的理解来解释两个模块 importlib模块 ...