# [noip 2012] 同余方程

$\because$ gcd(a, b) = 1
$\therefore$ a^phi(b) = 1 (mod b)
$\therefore$ x = a^(phi(b)-1) (mod b)

exgcd这么蛋疼东西我才不会呢！

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(!x) {putchar('0');return;}
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
while(sl) putchar(s[--sl]);
}

long long a, b, phi;

long long power_mod(long long x,int k)
{
long long r = 1;
while(k)
{
if(k&1) r *= x, r %= b;
x *= x, x %= b, k >>= 1;
}
return r;
}
int main()
{
long long  t;

t = phi = b;

for(int i = 2; i * i <= b; i++)
{
if(!(t % i))
{
phi /= i, phi *= i - 1;
while(!(t % i)) t /= i;
}
}
if(t != 1) phi /= t, phi *= t - 1;

write(power_mod(a, phi - 1));

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120