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& ...
随机推荐
- SmartWeatherAPI C#版
private string GetKey(string areaId, string type, string date, string appId, string privateKey) { va ...
- SSIS 抽取excel出错:所请求的 OLE DB 访问接口 Microsoft.ACE.OLEDB.12.0 尚未注册
如果是安装的office2010就要装这个,如果是2007就不用装! http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b ...
- Spring Cloud Hystrix 服务容错保护
目录 一.Hystrix 是什么 二.Hystrix断路器搭建 三.断路器优化 一.Hystrix 是什么 在微服务架构中,我们将系统拆分成了若干弱小的单元,单元与单元之间通过HTTP或者TCP等 ...
- 看懂物联网fr
看懂物联网 2015-10-11 物联网世界 1.第三次IT浪潮 互联网时代的特征是信息驱动了生产力,无论众包.订单式生产这些理论:还是B2C.O2O各类业务模式:归根结底,是信息优化了生产关 ...
- 渣渣菜鸡的 ElasticSearch 源码解析 —— 启动流程(上)
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/08/11/es-code02/ 前提 上篇文章写了 ElasticSearch 源码解析 -- ...
- java ReentranLock锁
1.效果和synchronized一样,都可以同步执行,lock方法获得锁,unlock方法释放锁 使用示例: package com.test; import java.util.concurren ...
- spring boot Filter过滤器的简单使用
springboot使用Filter过滤器有两种方式: 一种是实现Filter接口然后通过@Component注解向项目加入过滤器 另一种是通过配置类来配置过滤器 @Component public ...
- 一个例子说明Jsp三大重要内置对象的生命周期
此处Jsp的三大内置对象指:request,session以及application.他们共有的方法:setAttribute,getAttribute,方法名和方法作用都是相同的,但是作用范围不一样 ...
- BZOJ 1806: [Ioi2007]Miners 矿工配餐
ime Limit: 10 Sec Memory Limit: 64 MBSubmit: 910 Solved: 559[Submit][Status][Discuss] Description ...
- 51nod 1276 岛屿的数量
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 有N个岛连在一起形成了一个大的岛屿,如果海平面上升超过某些岛的高度时,则这个岛会被淹没 ...