Marshal's Confusion III(快速幂)

Marshal‘s Confusion III
Time Limit: 3000 MS Memory Limit: 65536 K
Total Submit: 236(62 users) Total Accepted: 74(51 users) Rating: Special Judge: No
Description
Marshallike to solve acm problems.But they are very busy, one day they meet a problem. Given three intergers a,b,c, the task is to compute a^(b^c))%317000011. so the turn to you for help. 
Input
The first line contains an integer T which stands for the number of test cases. Each case consists of three integer a, b, c seperated by a space in a single line. 1 <= a,b,c <= 100000
Output
For each case, print a^(b^c)%317000011 in a single line.
Sample Input
2 1 1 1 2 2 2
Sample Output

1 16

Author
#include<cstdio>
#include<cmath>
#include<queue>
#include<iostream>
using namespace std;
const long m=317000011;
long ksm(long x,long y,int m)//x^y
{
    long sum=x;
    long res=1;
    while(y>0)
    {
        if(y%2==1)
        {
            res=(res*sum)%m;
        }
        sum=(sum*sum)%m;
        y/=2;
        //cout<<y<<endl;
    }
    return res;
}
int main()
{
    int T;
    long a,b,c;
    while(~scanf("%d",&T))
    {
        while(T--)
        {
            scanf("%ld%ld%ld",&a,&b,&c);
            long ss=ksm(a,ksm(b,c,m),m);
            ss%=m;
            printf("%ld\n",ss);
        }
    }
//    cout<<ksm(2,1)<<endl;
}

Marshal's Confusion III(快速幂)

时间: 2024-08-25 12:00:02

Marshal's Confusion III(快速幂)的相关文章

VOJ 1067 Warcraft III 守望者的烦恼 (矩阵快速幂+dp)

题目链接 显然可知 dp[n] = dp[n-k] + dp[n-k+1] + ... +dp[n-1]; 然后要用矩阵来优化后面的状态转移. 也就是矩阵 0 1 0 0    a     b 0 0 1 0 * b =  c 0 0 0 1    c     d 1 1 1 1    d    a+b+c+d 然后跑快速幂 #include <iostream> #include <cstdio> #include <algorithm> #include <c

矩阵快速幂总结

基础:快速幂,矩阵乘法: 快速幂: #include<cstdio> #include<cstring> #define ll long long ll poww(ll a,ll b) { ll ans=1; while(b) { if(b&1) ans*=a; b>>=1;a*=a; } return ans; } int main() { ll a,b; scanf("%lld %lld",&a,&b); ll ans=p

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

快速幂取模(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

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

快速幂及快速幂取模

快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 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 } 快速幂取模需要记住一个定理:积的取模等于取模积的取模:算法是蒙

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

NYOJ127 星际之门(一)(最小生成数的个数+快速幂)

题目描述: http://acm.nyist.net/JudgeOnline/problem.php?pid=127 可以证明,修建N-1条虫洞就可以把这N个星系连结起来. 现在,问题来了,皇帝想知道有多少种修建方案可以把这N个星系用N-1条虫洞连结起来? 输入 第一行输入一个整数T,表示测试数据的组数(T<=100) 每组测试数据只有一行,该行只有一个整数N,表示有N个星系.(2<=N<=1000000) 输出 对于每组测试数据输出一个整数,表示满足题意的修建的方案的个数.输出结果可能

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st