这道题普通做法会发生溢出且会超时,应当用快速幂来求解。

快速幂讲解

 #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 (快速幂)的更多相关文章

  1. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

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

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

  3. 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 ...

  4. POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  5. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  7. POJ 3641 快速幂+素数

    http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...

  8. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  9. poj 3641 快速幂

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

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

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

随机推荐

  1. ubuntu网卡配置及安装ssh服务

    1.ubuntu网卡配置 1.查看网卡名称 ip a 2.进行编辑网卡配置文件 sudo vi /etc/network/interfaces 更改网卡配置文件添加内容修改内容如下:下面的enp0s3 ...

  2. multiprocessing中进程池,线程池的使用

    multiprocessing 多进程基本使用 示例代码1 import time import random from multiprocessing import Process def run( ...

  3. JavaScript深入之参数按值传递

    在<JavaScript高级程序设计>第三版 4.1.3,讲到传递参数: ECMAscript中所有函数的参数都是按值传递 按值传递 也就是,把函数外部的值复制给函数内部的参数,就和把值从 ...

  4. h5图片上传简易版(FileReader+FormData+ajax)

    一.选择图片(input的file类型) <input type="file" id="inputImg"> 1. input的file类型会渲染为 ...

  5. 配置一个nginx反向代理&负载均衡服务器

    一.基本信息 系统(L):CentOS 6.9 #下载地址:http://mirrors.sohu.com 反代&负载均衡(N):NGINX 1.14.0 #下载地址:http://nginx ...

  6. PHP通过curl向其它服务器发请求并返回数据

    在很多时候,我们都需要请求第三方的服务器来获取一些数据,比如token,比如百度的主动推送,那么我们的php如何实现向第三方服务器发请求呢?我们可以通过curl来实现 首先定义请求的url,然后创建h ...

  7. 通过devmem访问物理地址

    目录 1.写在前面 2.devmem使用 3.应用层 4.内核层 1.写在前面 最近在调试时需要在用户层访问物理内存,发现应用层可以使用devmem工具访问物理地址.查看源码,实际上是对/dev/me ...

  8. django-models 数据库取值

    django.shortcuts import render,HttpResponse from app01.models import * # Create your views here. def ...

  9. python 基础练习题, 陆续添加中

    判定用户输入数字是否为闰年 闰年的定义:能够被4整除的年份 #input是自定义输入内容的函数 year = input("请输入年份数字:") #xxx.isdigit方法是检测 ...

  10. python代理爬取存入csv文件

    爬取高匿代理 from urllib import request import re import time f = open('西1.csv','w',encoding='GBK') header ...