POJ1995 Raising Modulo Numbers(快速幂)

POJ1995 Raising Modulo Numbers

  计算(A1B1+A2B2+ ... +AHBH)mod M.

  快速幂,套模板

  

/*
* Created:     2016年03月30日 23时01分45秒 星期三
* Author:      Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int INF=0x3f3f3f3f;
ll mod;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}//快速幂计算
const bool AC=true;

int main()
{
    int t,n;
    ll ans,a,b;
    in(t); //别忘了输入
    while(t--){
    inlld(mod);
    in(n);
    ans=0;
    rep(i,0,n){
    inlld2(a,b);
    ans=(ans+powmod(a,b))%mod;
    }
    outlld(ans);
    }
    return 0;
}
时间: 2024-10-07 18:38:28

POJ1995 Raising Modulo Numbers(快速幂)的相关文章

POJ1995:Raising Modulo Numbers(快速幂取余)

题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { int

POJ 1995 Raising Modulo Numbers (快速幂模板)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4938   Accepted: 2864 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

poj Raising Modulo Numbers 快速幂模板

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8606   Accepted: 5253 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

Raising Modulo Numbers ---- 快速幂

Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5087 Accepted: 2959 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and

POJ 1995 Raising Modulo Numbers (快速幂)

题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL Ksm(LL a, LL b, LL p) { LL ans = 1; while(b) { if(b & 1) { ans = (ans * a) % p; } a = (a * a) % p; b >>= 1; } return ans; } int main() { LL p, a

poj1995 Raising Modulo Numbers【高速幂】

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5500   Accepted: 3185 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

POJ1995 Raising Modulo Numbers

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6373   Accepted: 3760 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

poj1995 Raising Modulo Numbers (快速幂,挑战有模版,纪念一下A的第一道快速幂)

/*快速幂,时间复杂度,数据范围*/ #include <iostream> #include <stdio.h> #include <string.h> using namespace std; long long a, b; int n, m; int sum, res; typedef long long ll; ll mod_pow(ll x, ll y, int z) { res = 1; while(y > 0){ if(y & 1) res

POJ1995 Raising Modulo Numbers【整数快速幂】

题目链接: http://poj.org/problem?id=1995 题目大意: N个人在一起玩游戏,每个人默写两个数字Ai.Bi,在同一个时间公开给其他玩家看.游戏的目的是 为了看谁能够在最快的时间求出所有的Ai^Bi的和对M取模的值.那么问题来了:你能够快速算出 (A1B1+A2B2+ ... +AHBH)mod M 的值吗? 思路: 用二分整数快速幂算法计算出每一个Ai^Bi,然后依次相加取模. AC代码: #include<iostream> #include<algorit