3456. Huge Numbers (Large)

单点时限: 4.0 sec

内存限制: 256 MB

Professor Shekhu has another problem for Akki today. He has given him three positive integers A, N and P and wants him to calculate the remainder when AN! is divided by P. As usual, N! denotes the product of the first N positive integers.

输入格式
The first line of the input gives the number of test cases, T. T lines follow. Each line contains three integers A, N and P, as described above.

Limits: 1≤T≤100.
Small dataset: 1≤A≤10,1≤N≤10,1≤P≤10.
Large dataset: 1≤A≤105,1≤N≤105,1≤P≤105.
输出格式
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the answer.

样例
input
2
2 1 2
3 3 2
output
Case #1: 0
Case #2: 1
提示
In Sample Case #1, the answer is the remainder when 21!=2 is divided by 2, which is 0.

In Sample Case #2, the answer is the remainder when 33!=36=729 is divided by 2, which is 1.

/*
思路:快速幂求a^b%p
阶乘次幂就是迭代求结果
*/
#include<iostream>
#define ll long long
using namespace std;
ll quickPow(ll a,ll b,ll p) {//快速幂
	ll ans=1;
	while(b) {
		if(b&1) {
			ans=ans*a%p;
		}
		a=a*a%p;
		b>>=1;
	}
	return ans;
}
int main() {
	int t;
	cin>>t;
	for(int i = 1; i <= t; i++) {
		ll a,n,p;
		cin>>a>>n>>p;
		ll ans=quickPow(a,n,p);
		for(int j = n-1; j >=1; j--) {
			ans=quickPow(ans,j,p)%p;
		}
		printf("Case #%d: %d\n",i,ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值