poj 1995 裸快速幂

1、 poj 1995  Raising Modulo Numbers

2、链接:http://poj.org/problem?id=1995

3、总结:今天七夕,来发水题纪念一下。。。入ACM这个坑也快一年了

题意:求ai^bi和模m。裸快速幂

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

int main()
{
    int z,n,m;
    LL a,b;
    LL sum,s;
    scanf("%d",&z);
    while(z--)
    {
        scanf("%d",&m);
        scanf("%d",&n);
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld",&a,&b);
            s=1;
            while(b)
            {
                if(b&1){       //b二进制最后一位是1就执行这步
                    s=(s*a)%m;  //即b为偶数不执行,b为奇数就让s累加b除2后剩下的
                }

                b/=2;
                a=(a*a)%m;     //即a^b=(a^2)^(b>>1)
            }

            sum+=(s%m);
        }
        printf("%lld\n",sum%m);
    }

    return 0;
}

时间: 2024-08-08 19:20:42

poj 1995 裸快速幂的相关文章

POJ 1995 (快速幂)

这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 1 #include <cstdio> 2 #include <cmath> 3 using namespace std; 4 int main(){ 5 int Z; 6 scanf("%d",&Z); 7 while(Z--){ 8 int M, H; 9 unsigned long long sum = 0; 10 scanf("%d%d",&M,&am

POJ 1995(快速幂)

1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 using namespace std; 10 int main(int argc,

BZOJ 1008 越狱 题解 裸快速幂

BZOJ 1008 越狱 题解 裸快速幂 1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7887  Solved: 3372[Submit][Status][Discuss] Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 Input 输入两个整数M,N.1<=M<

POJ 3070-Fibonacci(矩阵快速幂求斐波那契数列)

Fibonacci Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3070 Appoint description:  System Crawler  (2015-02-28) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 +

poj 3233(矩阵快速幂)

题目链接:http://poj.org/problem?id=3233: 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以很显然是矩阵快速幂,但有一点,本题k的值非常大,所以要用二分求和来减少运行时间. 代码: #include <set> #include <map> #include <stack> #include <queue> #include <math.h> #include <vector> #in

POJ 3070 矩阵快速幂

裸题,最简单fib的应用模板,算是新技能get 吧. 其实和快速幂差不多了,只是矩阵代替的递推式. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1005; 6 struct node 7 { 8 int a[2][2]; 9 void init() 10 { 11 a[0][0] = a[1][0] =

POJ 3070 矩阵快速幂解决fib问题

矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; #define MOD 10000 ll a[7],b[7],a0[7],b0[7]; void pow_mod(ll n) { a0[1]=a0[2]=a0[3]=1,a0[4]=0; b0[1]=b0[4]=

POJ 1845 Sumdiv (快速幂+质因数+约数和公式+同余模)

Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16109   Accepted: 3992 Description Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 99

Fibonacci (poj 3070 矩阵快速幂)

Language: Default Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10099   Accepted: 7211 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the