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& ...
随机推荐
- MyBatis学习总结(三)---映射文件及引入方式
MyBatis的强大,主要原于它强大映射功能,相对其它的jdbc,使用MyBatis,你会发现省掉很多代码.上一篇已经简单做出一个实例.今天就了解一下MyBatis的映射xml文件. 了解上一篇fri ...
- java进程占用系统内存高,排查方法
查看所有内存占用情况 top 定位线程问题(通过命令查看16764 进程的线程情况) ps p -L -o pcpu,pmem,pid,tid,time,tname,cmd 计数 ps p -L -o ...
- [20190611]记录一下github的基本用法
本文记录如何使用github创建项目并上传代码,因为有一段时间没用github了,中途又重装了系统,今天重新使用一下. 然后特地做简要记录: 1. 创建SSH Key SSH Key指一般在C:\Us ...
- Windows 8 / win8 拼音输入法/搜狗输入法 visual Studio 2010 / VS2010 不兼容
是visual assist X 的问题,更新到VA_X_Setup 2001 解决问题 老版本处理:Tools-->Extension Manager-->Uninstall
- specrate 与specspeed 的区别
What is the difference between a "rate" and a "speed" metric?There are several d ...
- deb软件安装
deb是debian linux的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...
- Effective C++ 重要条款
学习c++的童鞋们,这本书不能错过,最近在学校图书馆借来这本书,准备好好啃啃它了,先把它的基本内容过一遍吧. 改变旧有的的C习惯 条款1:尽量以const和inline取代#define. 条款2:尽 ...
- mac不限速下载百度网盘
本文转载自:https://blog.csdn.net/u010837612/article/details/80029212 相信大家都比较困惑,百度网盘客户端限速后一般只有几十K的下载速度,Win ...
- 监测元素resize
前言 近来有需求要做分页,听起来可能有点Low. 所以我要把Low的事情做得有点逼格. 分页本身没啥,但是数据量起来了,比如十万. 要是不做点处理, 那你的页面估计爽得很,机器也爽得很. 放心,我不会 ...
- Python封装补充
property属性 property实际是setter getter deleter是集合体,并不是一个单独的方法 import math # 使用的库 class Circle: def __in ...