题意:

思路:

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

#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. nmap学习之相关参数列表

    一.TARGET SPECIFICATION: Can pass hostnames, IP addresses, networks, etc. Ex: scanme.nmap.org, micros ...

  2. kali安装以及配置

    1.https://klionsec.github.io/2017/04/29/kali-config/ 2.http://www.freebuf.com/sectool/133526.html

  3. 20155215 2016-2017-2 《Java程序设计》第9周学习总结

    20155215 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 JDBC入门 - JDBC(Java DataBase Connectivity) ...

  4. QMessageBox消息框

    QMessageBox提供两套接口来实现,一种是static functions(静态方法调用),另外一种 the property-base API(基于属性的API) #需要 from PyQt5 ...

  5. Managers经理/代理形式的数据共享

    代理方式实现进程间共享字典和列表形式的数据, 代码块: import os from multiprocessing import Process,Manager def f(d,l,normalLi ...

  6. Django 2.0 学习(12):Django 模板语法

    Django 模板语法 一.模板 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法 模板语法变量:{{ }} 在Django模板中遍历复杂数据结构的关键是句点字 ...

  7. 一分钟了解contextlib模块

    cobtextlib模块用于简化上下文管理器,其内置装饰漆@contextmanager,我们通过编写一个被contextmanager装饰的generator来简化上下文管理. from conte ...

  8. TDateTimePicker 选择最小日期时异常处理

    TDateTimePicker 控件属性窗体选择最小日期,运行时选择时可以看到的最小的日期,但是选择最小时就异常 :date is less than minimum of  ***     解决过程 ...

  9. Python 爬虫学习

    #coding:utf-8 #author:Blood_Zero ''' 1.获取网页信息 2.解决编码问题,通过charset库(默认不安装这个库文件) ''' import urllib impo ...

  10. NSOperation 代码,阐述NSOperation一般功能和重要功能

    // // ViewController.m // 05-NSOperation // // Created by jerry on 15/9/5. // Copyright (c) 2015年 je ...