POJ 1995 (快速幂)
这道题普通做法会发生溢出且会超时,应当用快速幂来求解。
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int Z;
scanf("%d",&Z);
while(Z--){
int M, H;
unsigned long long sum = ;
scanf("%d%d",&M,&H);
for(int i = ; i < H; i++){
long long a,b;
scanf("%lld%lld",&a,&b);
long long ans = ;
while(b){
if(b&){
ans = (ans * a)%M;
b--;
}
b/=;
a = (a*a)%M;
}
sum += (ans%M);
}
printf("%llu\n",sum%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 快速幂
题意:给出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 ...
- 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& ...
随机推荐
- 【saltstack 集中化管理】
Master(监控端): Minion(被监控端) 监控: /etc/master: #interface:监控端地址 #自动接受被监控端证书 #saltstack文件根目录位置 #启动监控 被监控: ...
- 正则验证input输入,要求只能输入正数,小数点后保留两位。
<input type="number" step="1" min="0" onkeyup="this.value= thi ...
- QP-nano结构分析
QP-nano是QP的一个裁剪版本,是一个通用的.可移植的.超轻量级的事件驱动型框架.适用于像8051.PIC.AVR.MSP430.68HC01/11/12.R8C/Tiny等资源受限的8位和16位 ...
- STM32(10)——窗口看门狗
简介: 窗口看门狗(WWDG)通常被用来监测由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障.除非递减计数器的值在 T6 位 (WWDG->CR 的第六位)变成 0 ...
- TensorFlow实现线性回归
from __future__ import print_function import tensorflow as tf import numpy import matplotlib.pyplot ...
- C语言实现 "谁是凶手?"
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个.以下为4个嫌疑犯的供词.A说:不是我. a=0B说:是C. c=1 C说:是D. d=1D说:C在胡说 ...
- 查看 dll 是32位还是64位 的 bat
******* @echo offset work_path=./cd %work_path% for /R %%s in (*.dll) do ( echo %%~nxs call dumpbin ...
- mysql库地址
https://dev.mysql.com/downloads/connector/
- linq中group by 的用法
如下代码: var dates=(from p in points group p by p.LevelId into g select new { g.Key,g });之后 你会拿到这个数组: 之 ...
- PostgreSQL 使用总结
1. USING的使用 USING是个缩写的概念:它接收一个用逗号分隔的字段名字列表, 这些字段必须是连接表共有的,最终形成一个连接条件,表示这些字段对必须相同. USING (a, b, c) 等效 ...