FZU 1759 Super A^B mod C

本文介绍了一种解决高次幂取模问题的有效算法,并提供了两种实现方式:一种利用欧拉函数进行优化,另一种采用逐位乘方的方法。适用于处理大规模数值计算场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).

Input

There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.

Output

For each testcase, output an integer, denotes the result of A^B mod C.

Sample Input

3 2 4
2 10 1000

Sample Output

1

24

高次幂取模,套公式搞定。

(B>=phi(C))

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
LL n, m, x;
char s[N];

LL phi(LL x)
{
	LL res = 1;
	for (LL i = 2; i*i <= x; i++)
	{
		if (x%i) continue;
		res *= i - 1;
		for (x /= i; !(x%i); res *= i, x /= i);
	}
	return res * max(x - 1, 1LL);
}

void get()
{
	if (strlen(s) <= 10) { sscanf(s, "%lld", &x); return; }
	LL g = phi(m);
	x = 0;
	for (int i = 0; s[i]; i++) x = (x * 10 + s[i] - '0') % g;
	x = x + g;
}

int main()
{
	while (scanf("%lld%s%lld", &n, s, &m) != EOF)
	{
		LL ans = 1;	  get();
		for (; x; x >>= 1)
		{
			if (x & 1) (ans *= n) %= m;
			(n *= n) %= m;
		}
		printf("%lld\n", ans % m);
	}
	return 0;
}

也可以直接用十进制快速幂,效率稍低

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define inone(x) scanf("%d", &x);
#define intwo(x,y) scanf("%d%d", &x, &y);
using namespace std;
typedef unsigned long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
int n, m;
char s[N];

int main()
{
	while (scanf("%d%s%d", &n, s, &m) != EOF)
	{
		int ans = 1;
		per(i, strlen(s) - 1, 0)
		{
			int res = n;
			rep(j, 1, 9)
			{
				if (s[i] - '0' == j) ans = 1LL * ans * res % m;
				res = 1LL * res * n % m;
			}
			n = res;
		}
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值