题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M.

思路:快速幂

实例

3^11  11=2^0+2^1+2^3    =》 3^1*3^2*3^8=3^11

实现代码:

int solve(int a,int b)

{ int ans=1;    

     while(b){ if(b&1)  ans=ans*a;   a=a*a;  b>>=1;}

}

解释一下代码:b&1即二进制表达式的最后一位,  11二进制为 1011 b>>=1 去掉二进制的最后一位

模的运算  (a^b)%p=(a%p)^b%p

解决问题的代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll mod_m(ll a, ll b, int p)
{
ll ans = 1ll;
a %= p;
while (b)
{
if (b & )
ans = (ans*a) % p;
a = (a*a) % p;
b >>= ;
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll ans = 0ll;
int m;
scanf("%d", &m);
int h;
scanf("%d", &h);
while (h--)
{
ll a, b;
scanf("%lld%lld", &a, &b);
ans += mod_m(a, b, m);
}
printf("%lld\n", (ans%m));
}
return ;
}

poj 1995 快速幂的更多相关文章

  1. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

  2. Raising Modulo Numbers(POJ 1995 快速幂)

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: ...

  3. POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  4. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  6. POJ 3641 快速幂+素数

    http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...

  7. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  8. poj 3641 快速幂

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  9. POJ 1995 Raising Modulo Numbers(快速幂)

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...

随机推荐

  1. pom文件jar包缺失问题

    一般情况,不在中央仓库的jar包,比如自己的版本,要用的话打入maven私服 在eclipse中引入其他项目(包含jar包),类似于放入了私服,删除工程源文件,会导致别的工程引用此jar包失效

  2. C. Journey

    C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. 使用jstack分析解决进程死锁问题

    项目启动后不久就会出现死锁的现象,一直不知道什么原因造成的,后来经过大神的指点,解决了这个问题. 流程如下: 1.环境jdk1.6以上: 2.linux下使用ps aux|grep tomcat 命令 ...

  4. 洛谷CF784E Twisted Circuit

    (本题本来可能也就普及难度但是硬生生给评成了一道NOI难度的紫题,有点无语...) 这道题目是一道愚人节题目,本来只有下面的电路图,结果翻译完之后难度就直接没了. ------------ 言归正传, ...

  5. If people in the communications only think about gains and losses of interest, then the pleasure of knowing each other will cease to exist.

    If people in the communications only think about gains and losses of interest, then the pleasure of ...

  6. 理解 JavaScript 的 async/await

    随着 Node 7 的发布,越来越多的人开始研究据说是异步编程终级解决方案的 async/await.我第一次看到这组关键字并不是在 JavaScript 语言里,而是在 c# 5.0 的语法中.C# ...

  7. JFinal视频教程

    最近开始录制JFinal视频教程,发布在腾讯课堂上,免费公开面向JFinal开发者,作为JFinal开发者入门学习.实际项目用遇到问题寻找解决方案的最好途径. 目前JFinal课程已经开始更新. 腾讯 ...

  8. npm相关命令

    npm install npm install log4js npm list npm list log4js #查看模板安装版本 npm install log4js@1.0.1 #指定模块版本安装 ...

  9. Easyui validatebox后台服务端验证

    Easyui validatebox的验证提示十分好用,可是在实际项目的运用中,经常会遇到需要服务器验证后并返回验证结果信息,比如验证用户名.手机号.邮箱是否已存在.于是就想着怎么拓展Easyui的验 ...

  10. log4cpp安装使用

    1. 主页:http://log4cpp.sourceforge.net“Log4cpp is library of C++ classes for flexible logging to files ...