poj 1995 快速幂
题意:给出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 快速幂的更多相关文章
- POJ 1995 快速幂模板
http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
- POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
- POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)
Sumdiv Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
题意: 给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
- POJ 3641 快速幂+素数
http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...
- Pseudoprime numbers(POJ 3641 快速幂)
#include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...
- poj 3641 快速幂
Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...
- POJ 1995 Raising Modulo Numbers(快速幂)
嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...
随机推荐
- shell 获得调用的python脚本的print值和错误log
1. shell 获得调用的python脚本的print值 python test.py > out.log 2.shell 获得调用的python脚本的错误log python test.py ...
- MCS-51单片机的定时器/计数器概念
一.MCS-51单片机的定时器/计数器概念 单片机中,脉冲计数与时间之间的关系十分密切,每输入一个脉冲,计数器的值就会自动累加1,而花费的时间恰好是1微秒;只要相邻两个计数脉冲之间的时间间隔相等,则计 ...
- golang学习资料
http://yougg.github.io/static/gonote/GolangStudy.html
- setTimeout() 实现程序每隔一段时间自动执行
定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 Ja ...
- Hbase region查找过程
HBase的table是该region切分的,client操作一个row的时候,如何知道这个row对应的region是在哪台Region server上呢?这里有个region location过程. ...
- Exchange DSAccess 事件分析
本文介绍了如何使用事件 ID 2080 来帮助诊断 Exchange DSAccess 问题中所包含的信息. 许多朋友经常遇到Active Directory 域和Exchange 服务器通信问题.那 ...
- 如何检查SQL Server索引填充因子
假如您有一个盛满水的玻璃杯,您要尝试再向这个玻璃杯中加水.结果会怎样呢?水会溢出来. SQL Server 的情况也是如此.当索引页填充满时,如果尝试添加新行,则 SQL Server 会将大约一半的 ...
- jquery的uploadify插件实现的批量上传V3.2.1版
你需要如下配置(包括引入文件)HTML: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&quo ...
- UVA 10382 Watering Grass (区间覆盖,贪心)
问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...
- Cross-Entropy Loss 与Accuracy的数值关系(很重要,很好的博客)
http://www.cnblogs.com/dengdan890730/p/6132937.html