Enormous Carpet (快速幂取模)

Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers. Lately, He bought a very very very large square carpet that has an enormous area, so he stopped amazed as to how large is this carpet exactly… Unfortunately, Ameer has a small length measurement tool, so he can’t measure the area of the carpet as a whole. However, Ameer has a very smart algorithm for folding a square piece of paper reducing it to an exact fraction of its original size, and then he came up with another intelligent algorithm for measuring the area of the carpet. Ameer decided to fold the carpet N times, each time reducing it to 1/K of its remaining area, After that he would measure the remaining area of the carpet and apply his algorithm to calculate the original area. As Ameer is still a beginner problem solver he wants to check whether his algorithm is correct. Also, since the final answer might be incredibly large, Ameer wants to check the remainder of the answer over several prime numbers of his choosing. Can you help Ameer getting the correct answer so that he can compare it with his own ?

Input

For each test case, you would be given three space separated integers on the first line N, K and A respectively, Where N and K are as described earlier and A is the area that Ameer has measured after folding the carpet N times. In the second line there will be an integer number C. The third line contains C integer prime numbers where the i-th number is called Pi. After the last test case, there will be a line containing three zeroes separated by a single space. 1 ≤ N, K, A ≤ 231 1 ≤ C ≤ 100 2 ≤ Pi < 231

Output

For each test case you should output on the first line “Case c:” where ‘c’ is the case number, then one line containing ‘C’ space separated integers on a line where the i-th integer is the remainder of the original area over Pi

Sample Input

Input
3 3 6
3
41 71 73
0 0 0
Output
Case 1:
39 20 16

题目大意:Ameer有一张面积很大的毯子,但是Ameer的测量工具测不了,于是,Ameer想出一个测量方法,她先将毯子折叠n次,每次折叠后面积都变为原来的k分之一,然后用测的折叠n次后的面积为a,
现在他想验证这个方法是否正确,于是Ameer给出c个数,要你编程输出毯子原面积对c个数取模后的结果。
样例的毯子原面积为6*3^3=162,所以结果为162%41=39,162%71=20,162%73=16;

因为数字可能会很大,会爆掉,所以用快速幂取模

推荐博客:http://blog.csdn.net/xuruoxin/article/details/8578992

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;

const int INF=0x3f3f3f3f;
const int MX=100+10;

LL PowerMod (LL a,LL b,LL c,LL d){  //返回长整型
    LL ans=d;
    a%=c;
    while (b>0){
        if (b%2==1) ans=(ans*a)%c;
        b/=2;
        a=(a*a)%c;
    }
    return ans;
}
int main(){
    LL n,k,a
    int x=1;
    while (~scanf ("%d %d %d",&n,&k,&a)){
        if (n==0&&k==0&&a==0) break;
        int c;
        scanf ("%d",&c);
        LL num[110];
        for (int i=0;i<c;i++) scanf ("%I64d",&num[i]);
        printf ("Case %d:\n",x++);
        for (int i=0;i<c;i++)
            if (i==c-1) printf ("%I64d\n",PowerMod(k,n,num[i],a));
            else printf ("%I64d ",PowerMod(k,n,num[i],a));
    }
    return 0;
}

 

posted @ 2016-08-09 22:29  Alex的ACM  阅读(402)  评论(0编辑  收藏  举报