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\) 和一个合法 ...
随机推荐
- 项目压力测试软件 -- LoadRunner 11.0 的安装、汉化和破解
重要说明: LoadRunner 11.0 只支持Win7,32位系统:不支持Win7,64位系统[ Win7,64位 我反复安装都没有成功!] 一.下载安装.汉化.破解文件: 我的下 ...
- caffe中的Accuracy+softmaxWithLoss
转:http://blog.csdn.net/tina_ttl/article/details/51556984 今天才偶然发现,caffe在计算Accuravy时,利用的是最后一个全链接层的输出(不 ...
- CentOS 6.5 下 QT4 连接 mysql 数据库的步骤
QT4 的安装请参考: CentOS 6.5 下安装 QT 4 mysql 的安装请参考: CentOS 6.5 下安装配置 mysql 1. 预防万一,先安装一下mysql-devel(一定要装!) ...
- ubuntu启动脚本
下午分析了一下mysql的启动脚本,找到这篇,记录一下,目前很多服务都是以这种方式封装,后面自己写来借鉴一下 http://blog.fens.me/linux-upstart/
- react+propTypes
React.createClass({ propTypes: { // 可以声明 prop 为指定的 JS 基本数据类型,默认情况,这些数据是可选的 optionalArray: React.Prop ...
- python 基础--pip(转)
感谢:http://www.cnblogs.com/csucat/p/4897695.html python需要用到一些其他的库,可以手动去各个库的官网下载,自己安装:也可以安装pip,使用pip可以 ...
- Ubuntu12.04 安装LAMP及phpmyadmin
1.安装 Apache apt-get install apache2 2.安装 PHP5 apt-get install php5 libapache2-mod-php5 3.安装 MySQL ap ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- 获取天气api
http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100通过城市id获得天气数据,xml文件数据,当错误时会有<error>节点http: ...
- hdu 1598 find the most comfortable road (并查集+枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...