题意:

思路:

对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模。

#include <cstdio>

typedef long long LL;

LL Ksm(LL a, LL b, LL p) {
LL ans = 1;
while(b) {
if(b & 1) {
ans = (ans * a) % p;
}
a = (a * a) % p;
b >>= 1;
}
return ans;
} int main() {
LL p, a, b;
int T;
int n;
scanf("%d", &T);
while(T--) {
scanf("%lld%d", &p, &n);
LL ans = 0;
while(n--) {
scanf("%lld%lld", &a, &b);
ans = (ans + Ksm(a, b, p)) % p;
}
printf("%lld\n", ans);
}
return 0;
}

POJ 1995 Raising Modulo Numbers (快速幂)的更多相关文章

  1. POJ 1995:Raising Modulo Numbers 快速幂

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

  2. poj 1995 Raising Modulo Numbers【快速幂】

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

  3. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  4. poj 1995 Raising Modulo Numbers 题解

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

  5. POJ 1995 Raising Modulo Numbers 【快速幂取模】

    题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...

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

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

  7. POJ 1995 Raising Modulo Numbers

    快速幂取模 #include<cstdio> int mod_exp(int a, int b, int c) { int res, t; res = % c; t = a % c; wh ...

  8. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  9. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

随机推荐

  1. 为什么要用dubbo,dubbo和zookeeper关系

    为什么要用dubbo? 当网站规模达到了一定的量级的时候,普通的MVC框架已经不能满足我们的需求,于是分布式的服务框架和流动式的架构就凸显出来了.     单一应用架构 当网站流量很小时,只需一个应用 ...

  2. Java编程思想 学习笔记8

    八.多态  在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三种基本特征. 多态通过分离做什么和怎么做,从另一角度将接口和实现分离开来. “封装”通过合并特征和行为来创建新的数据类型.“实现 ...

  3. 深度神经网络tricks and tips

    1)data augmentation (augment 增加,aug:to increase 词根,同August(奥古斯特即凯撒大帝,自认为最伟大的帝王,他出生在八月,他以自己的名字命名这个月)同 ...

  4. Nginx 中 FastCGI 配置示例

    nginx 中 FastCGI 参数:主要是在 http 层 :保证PHP环境的高校运行 主要对PHP用来解析 fastcgi_cache_path /tmp/fastcgi_cache levels ...

  5. JS堆栈与拷贝

    一.堆栈的定义 1.栈是一种特殊的线性表.其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行. 结论:后进先出(Last In First Out),简称为LIFO线性表.栈的应用有:数制 ...

  6. js实现获取两个日期之间所有日期最简单的方法

    Date.prototype.format = function() { var s = ''; var mouth = (this.getMonth() + 1)>=10?(this.getM ...

  7. SpringBoot处理静态资源的两种方式

    静态资源是指----> CSS.JS之类的文件 首先创建SpringBoot Web项目 添加Spring Boot Web Starter <dependency> <gro ...

  8. Linux之V4L2基础编程【转】

    转自:https://www.cnblogs.com/emouse/archive/2013/03/04/2943243.html 本文内容来源于网络,本博客进行整理. 1. 定义 V4L2(Vide ...

  9. 无向图最小割Stoer-Wagner算法学习

    无向连通网络,去掉一个边集可以使其变成两个连通分量则这个边集就是割集,最小割集当然就权和最小的割集. 使用最小切割最大流定理: 1.min=MAXINT,确定一个源点 2.枚举汇点 3.计算最大流,并 ...

  10. 整理一下odoo10在windows系统下部署的流程

    odoo10环境搭建 所需依赖: Python3.5 odoo10.0 Node.js PostgreSQL 9.5 PyCharm 专业版 1.首先先安装好Python3.5,并设置好环境变量 2. ...