CF# 334 Moodular Arithmetic
2015-12-31 11:48
399 查看
B. Moodular Arithmetic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that
View Code
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that
/** Create By yzx - stupidboy */ #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <deque> #include <vector> #include <queue> #include <iostream> #include <algorithm> #include <map> #include <set> #include <ctime> #include <iomanip> using namespace std; typedef long long LL; typedef double DB; #define MIT (2147483647) #define INF (1000000001) #define MLL (1000000000000000001LL) #define sz(x) ((int) (x).size()) #define clr(x, y) memset(x, y, sizeof(x)) #define puf push_front #define pub push_back #define pof pop_front #define pob pop_back #define mk make_pair inline int Getint() { int Ret = 0; char Ch = ' '; bool Flag = 0; while(!(Ch >= '0' && Ch <= '9')) { if(Ch == '-') Flag ^= 1; Ch = getchar(); } while(Ch >= '0' && Ch <= '9') { Ret = Ret * 10 + Ch - '0'; Ch = getchar(); } return Flag ? -Ret : Ret; } int p, k; vector<int> factor; inline void Input() { scanf("%d%d", &p, &k); } inline int Power(int b, int t, int mod = 1000000007) { int ret = 1; while(t) { if(t & 1) ret = (1LL * ret * b) % mod; b = (1LL * b * b) % mod, t >>= 1; } return ret; } inline void Ext_Gcd(int a, int b, int &x, int &y) { if(b == 0) x = 1, y = 0; else { Ext_Gcd(b, a % b, x, y); int t = x; x = y; y = t - (a / b) * x; } } inline void Solve() { if(k == 0) { printf("%d\n", Power(p, p - 1)); return; } if(k == 1) { printf("%d\n", Power(p, p)); return; } /*int x, y; Ext_Gcd(k, p, x, y); if(y <= 0) { int t = y / k + 1; x -= t * p, y += t * k; } int s; LL t; for(s = 1, t = k; ((t - x) % p + p) % p != 0; t *= k, s++) ;*/ int t = p - 1; for(int i = 1; i * i <= t; i++) if(t % i == 0) { factor.pub(i); factor.pub(t / i); } sort(factor.begin(), factor.end()); int len = factor.size(), s; for(int i = 0; i < len; i++) if(Power(k, factor[i], p) == 1) { s = factor[i]; break; } int ans = Power(p, (p - 1) / s); printf("%d\n", ans); } int main() { freopen("a.in", "r", stdin); Input(); Solve(); return 0; }
View Code
相关文章推荐
- android sqlite导入数据
- PHP 7.0 安装使用与性能监测!
- 初学django1-django模型中自定义方法从身份证号码中获取出生日期
- js与jsp
- Android Canvas drawText()文字居中
- Jsp中解决session过期跳转到登陆页面并跳出iframe框架的方法
- Android四种启动模式及其注意事项
- Sublime Text 全程指南
- another lua vm
- NSMutableArray的使用... exchangeObjectAtIndex
- cvBlob 作为静态库链接问题
- 创建自己的内存管理
- eclipse编码
- Ubuntu 14.04 下的MAC OS X 主题安装
- SenchaExt中的DataView实现table布局效果
- 2015年总结
- Redis和Jedis简单使用示例
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- 云计算公司如何打败亚马逊AWS?
- android应用清除应用缓存原理