您的位置:首页 > 其它

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

/**
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: