首页 > 其他 > 详细

poj 1995 裸快速幂

时间:2016-08-10 00:52:01      阅读:189      评论:0      收藏:0      [点我收藏+]

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;
}
View Code

 

poj 1995 裸快速幂

原文:http://www.cnblogs.com/sbfhy/p/5755147.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!