s
u
p
e
r
a
b
m
o
d
c
super\ a^b\ mod\ c
super ab mod c
题目大意:
给定一个整型a, 数组b, 模数c, 求
a
b
m
o
d
c
a^b\ mod\ c
ab mod c
考察对欧拉定理的降幂应用,直接将每一位都进行取模即可
代码如下:
#include <cstdio>
#include <cstring>
#include <cctype>
#define pk putchar(' ')
#define ph puts("")
using namespace std;
typedef long long ll;
template <class T>
void rd(T &x)
{
x = 0;
int f = 1;
char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= f;
}
template <class T>
void pt(T x)
{
if (x < 0)
putchar('-'), x = (~x) + 1;
if (x > 9)
pt(x / 10);
putchar(x % 10 ^ 48);
}
int phi(int x)
{
int res = x;
for (int i = 2;i * i <= x; i++)
if (x % i == 0)
{
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1)
res = res / x * (x - 1);
return res;
}
const int N = 1e6 + 5;
int a, mod;
char b[N];
int qkpow(int a, ll b)
{
int res = 1;
while (b)
{
if (b & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return res;
}
int main()
{
while (scanf ("%d %s %d", &a, b, &mod) != EOF)
{
int i, len = strlen(b), pc = phi(mod);
ll now = 0;
for (i = 0;i < len; i++)
{
now = now * 10 + b[i] - 48;
if (now > pc)
break;
}
if (i == len)
pt(qkpow(a, now)), ph;
else
{
now = 0;
for (int i = 0;i < len; i++)
now = (now * 10 + b[i] - 48) % mod;
pt(qkpow(a, now + pc)), ph;
}
}
return 0;
}