Enormous Carpet

本文介绍了一个有趣的问题:如何计算一块经过多次折叠后的巨大地毯的原始面积,并通过快速幂算法求解该问题。针对每个测试案例,输出地毯原始面积对若干质数取模的结果。
                                  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;
}
尊敬的访问者, Dear visitor, Chère cliente, 訪問者へ 、 您好! Hello! Bonjour! こんにちは ! 我们检测到您所在的子网和/或所使用的客户端存在大量下载某些较大二进制文件的行为,为保证用户的正常使用,我们阻断了此类请求。 We have detected enormous traffic from your network or client and have blocked your requests to ensure the quality of service for normal users. Nous avons détecté un grand nombre de téléchargements de certains fichiers binaires volumineux à partir de votre sous-réseau et/ou du client que vous utilisez et avons bloqué ces requêtes pour garantir un accès normal aux utilisateurs. お客様のサブネットおよび/またはクライアントで、特定の大きなバイナリのダウンロードを多数検出しました。お客様の通常の使用のためこれらのリクエストをブロックさせていただきます。 您可以尝试更改网络环境或更换客户端;您也可以联系 support@tuna.tsinghua.edu.cn 并附上下方的标识符。 You could try another network or client, or have your network administrator contact support@tuna.tsinghua.edu.cn with the identifier below. Vous pouvez essayer de modifier votre environnement réseau ou de changer de client ; vous pouvez également contacter support@tuna.tsinghua.edu.cn avec l'identifiant ci-dessous. ネットワーク環境の変更、クライアントの変更などをお試しください。また、下記の識別子を用いて support@tuna.tsinghua.edu.cn までご連絡ください。 00000071 020b64314e52834258ac4af3c0b60b7e安装R语言
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值