CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Gym 100935D

Description

standard input/output
Statements

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 6341 71 730 0 0

Output

Case 1:39 20 16

比赛时候没模板 也没看懂题目什么意思 感觉智商被掏空....题意就是把原面积求出来后对后面给的数取模   (然而我看了题目半个小时还不知道是要干什么

拿了学长的一个模板 今天看了岛娘的直播  刚好学长模板也是岛娘那种风格 试了一下 感觉清晰了一点 但感觉还是很不习惯  
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;

LL fastpow(LL a,LL b,LL c,LL mod){
    LL res = a;
    while(c){
        if(c & 1) res = (res * b) % mod;
        b = (b * b) % mod;
        c >>= 1;

    }
    return res % mod;
}

int main()
{
    //FIN
    int cas = 1;
    LL n, k, a;
    while(~scanf("%I64d%I64d%I64d", &n, &k, &a))
    {
        if(n == 0 && k == 0 && a==0)  break;
        int c;
        LL num[110];
        scanf("%d", &c);
        for(int i = 0;i < c; i++)
            scanf("%I64d", &num[i]);
        printf("Case %d:\n",cas++);
        for(int i = 0;i < c; i++){
            if(i == 0)  printf("%I64d", fastpow(a, k, n, num[i]));
            else  printf(" %I64d", fastpow(a, k, n, num[i]));
        }
        printf("\n");

    }

}

  

时间: 2024-08-02 00:18:17

CodeForces Gym 100935D Enormous Carpet 快速幂取模的相关文章

快速幂取模(POJ 1995)

http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c=((a%c)*b)%c 每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 由此可以用快速幂算法优化: http://www.cnblogs.com/qlky/p/5020402.html 再结合取模公式: (a + b) % p = (a % p

快速幂及快速幂取模

快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log?N), 与朴素的O(N)相比效率有了极大的提高.——bybaidu 快速幂可以用位运算这个强大的工具实现. 代码: 1 int pow(int a,int b) 2 { 3 int ans=1; 4 while(b!=0) 5 { 6 if(b&1) 7 ans*=a; 8 a*=a; 9 b>>=1; 10 } 11 return ans; 12 } 快速幂取模需要记住一个定理:积的取模等于取模积的取模:算法是蒙

关于快速幂取模

今天看算法书的时候,看到一道关于快速幂取模的题,心想好像不难,便写了一下,发现我的渣渣代码写的比正常的O(N)复杂度还要慢(天知道我怎么做到的T_T),渣渣代码如下: 1 public static long fastMi(long x,long n){ 2 if(n==1){ 3 return x; 4 } 5 if(n%2==0){ 6 return fastMi(x,n/2)*fastMi(x,n/2); 7 }else{ 8 return fastMi(x,n/2)*fastMi(x,n

快速幂取模算法

什么是快速幂? 快速幂应当是快速幂取模的简称 对于一般的求幂算法,求$a^b\,\bmod\,m$,即使用循环b次的方法,复杂度是$O(b)$的,当b很大的时候,这种算法就会显得十分缓慢. 快速幂是基于以下明显的事实: $${a^b} \equiv {(a^2)^{\frac{b}{2}}} \pmod{m}\quad b\ is\ even$$ $${a^b} \equiv {(a^2)^{\frac{b}{2}}*a} \pmod{m}\quad b\ is\ odd$$ 那么我们得到这样一

快速幂取模

参考文章来源:Reait  Home(http://www.reait.com/blog.html) 转载请注明,谢谢合作. 在Miller Rabbin测试素数,就用到了快速幂取模的思想.这里总结下.求a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 算法1:利用公式a*b%c=((a%c)*b)%c,这样每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 代码如下: 01.int modexp_simpl

快速幂取模和快乘取模

一.快速幂取模概念 快速幂取模,顾名思义,就是快速的求一个幂式的模(余),比如a^b%c,快速的计算出这个式子的值. 在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法. 二.快速幂取模算法实现 1)很容易能想到,循环b次,每次乘a,最后对c取余就可以了. int ans = 1; for(int i = 1; i<=b; i++) { ans = ans * a; } ans = ans % c; 这个朴素算法的问题是: 1.如果a和b

HDU 4365 正方形格子涂色中心对称轴对称的涂法有多少种-思维-(矩阵坐标关系&amp;快速幂取模)

题意:n*n的格子,涂色,有k种颜料,必须满足旋转任意个90度和翻转之后图片的样子不变,现在已经有m个格子涂过色了,问还有多少种涂法满足上述条件. 分析: 满足上述对称条件,那么涂色的种类问题我们可以放在正方形的一个角来做,因为一个角确定了其他角的颜色也就确定了. 以左上角的下半角为例.共有1+2+....+(n+1)/2个格子,然后记录涂过色的格子对应到这个三角形里的格子数目,用tot来记录,即每输入一个涂过色的格子的坐标我们就在这个三角形里找与之对应的坐标,用vis[][]数组标记是否已经计

CSU - 1556 Jerry&#39;s trouble(快速幂取模)

[题目链接]:click here~~\ [题目大意]:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < 1000) [解题思路]: 快速幂取模 代码: #include<bits/stdc++.h> #define LL long long using namespace std; const LL mod=(LL)1e9+7; LL pow_mod(LL a,LL p,LL n) { i

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列