admin管理员组

文章数量:1582642

                                  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

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

题目大意:有一块超大的地毯,折叠n次,每次面积变为原来的k分之一,最后面积是为a,问该地毯的面积对c去模之后的结果;

思路:快速幂。对每一个c,做一次快速幂即可;注意用longlong;

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define maxn 110

LL kuaisumi(LL a,LL b,LL c,LL d)
{
    LL ans = d;
    a = 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 tt = 0;
    while(~scanf("%I64d%I64d%I64d",&n,&k,&a))
    {
        if(n==0&&k==0&a==0) break;
        int c;
        scanf("%d",&c);
        LL tmp[110];
        for(int i=1;i<=c;i++) scanf("%I64d",&tmp[i]);
        printf("Case %d:\n",++tt);
        for(int i=1;i<=c;i++)
        {
           if(i == c)
            printf("%I64d\n",kuaisumi(k,n,tmp[i],a));
           else
            printf("%I64d ",kuaisumi(k,n,tmp[i],a));
        }
    }
    return 0;
}

本文标签: EnormousCarpet