Kickstart Round G 2017 Problem A. Huge Numbers

9 篇文章 0 订阅

roblem A. Huge Numbers

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
9 points
Large input
14 points

Problem

Professor Shekhu has another problem for Akki today. He has given him three positive integers AN 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.

Input

The first line of the input gives the number of test cases, TT lines follow. Each line contains three integers AN and P, as described above.

Output

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.

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.

Sample


Input 
 

Output 
 
2
2 1 2
3 3 2

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,n,p  求 a^(N!) %p的结果

思路:看数据量,N是可以枚举求的,开始往欧拉函数想,想不下去了。。。。

后来发现化简一下就可以了

a^(N!)%p = a^(1*2*3*...*N)%p = (...(a^1)^2...)^N%p

每层求幂都带着%p,这样枚举1~N,然后每个求幂用快速幂就可以了。

#include <bits/stdc++.h>
using namespace std;
const int N = 310;

#define PI acos(-1)
typedef long long ll;
#define INF 0x3f3f3f3f
int mod = 1000000007;

ll Pow(ll a, int x, int p)
{
	ll ret = 1;
	while (x)
	{
		if (x&1)
			ret = (ret*a)%p;
		a = (a*a)%p;
		x >>= 1;
	}
	return ret;
}
void run()
{ 
	 ll a;
	 int n, p;
	 scanf("%lld%d%d", &a, &n, &p);
	 for (int i = 1; i <= n; i++)
	 {
	 	a = Pow(a, i, p);//求得的结果作为新的底数
	 }
	 printf("%lld\n", a);
}

int main()
{
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int T, cas = 1;
	scanf("%d", &T);
	
	while (T--)
	{ 
		printf("Case #%d: ", cas++);
		run();
	}
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值