51nod 1135 原根
来源:互联网 发布:燕十八mysql教程 网盘 编辑:程序博客网 时间:2024/06/14 14:15
题意:给出素数p,求最小的原根
复习下原根的定义:一般来说最小的原根会比较小,所以可以从小到大枚举,在数据较小的时候可以这么做
只需检查(p - 1)所有的素因子即可,因为若gcd(a, m) == 1,切a ^ g % m == 1,则必有c | d,其中c是最小的是的a ^ c % m成立的正整数。
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135
#include <bits/stdc++.h>using namespace std;typedef long long ll;vector<ll> prime_factor(ll p){ ll m = floor(sqrt(p * 1.0) + 0.5); vector<ll> res; for(ll i = 2; i <= m; i++) { if(p % i == 0) { res.push_back(i); while(p % i == 0) p /= i; } } if(p > 1) res.push_back(p); return res;}ll pow_mod(ll x, ll n, ll mod){ ll res = 1; while(n) { if(n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res;}bool g_test(ll g, ll p, const vector<ll> &v){ int m = v.size(); for(int i = 0; i < m; i++) if(pow_mod(g, (p - 1) / v[i], p) == 1) return false; return true;}ll primitive_root(ll p){ ll res = p - 1; vector<ll> v = prime_factor(res); ll g = 1; while(true) { if(g_test(g, p, v)) return g; g++; }}int main(){ ll p; while(cin >> p) { cout << primitive_root(p) << endl; } return 0;}
0 0
- 51nod 1135 原根
- 51nod 1135 原根
- 【51nod 1135 原根】
- 51nod-1135:原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51 Nod 1135 原根
- 51 NOD 1135 原根
- 51nod 1135原根
- 51Nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51Nod-1135-原根
- 51nod 1135 原根
- 51Nod 1135 原根
- 51Nod-1135-原根
- ICollection接口
- ROS下使用rplidar运行google cartographer
- json数组解析
- poj3809 Twenty Questions
- Binder service入门–创建native binder service
- 51nod 1135 原根
- 动态在代码中设置权重
- C++隐藏代码,被隐藏的代码不被执行
- 值类型和引用类型
- 图片处理方法总结
- 服务器重装
- poj 2680 Computer Transformation (dp+高精度)
- jq获取复选框的值(全选,反选等)
- Thinkphp5 数据分页显示