Raising Modulo Numbers
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9745   Accepted: 5921

Description

People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow: 

Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions AiBi from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers. 

You should write a program that calculates the result and is able to find out who won the game. 

Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.

Output

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression 

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13

参考代码:

#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
ll mod_pow(ll x,ll n,ll mod)//快速幂运算
{
    ll res=1;
    while(n>0)
    {
        if(n&1)res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
int MOD;
int N;
int A,B;
int main()
{
    //freopen("C://Users/Administrator/Desktop/acm.txt","r",stdin);
    int test;
    cin>>test;
    ll ans=0;
    while(test--)
    {
        ans=0;
      cin>>MOD>>N;
        while(N--)
        {
            cin>>A>>B;
            ans=(ans+mod_pow(A,B,MOD))%MOD;
        }
        cout<<ans<<endl;
    }
    return 0;
}

POJ 1995(有关快速幂运算的一道水题)的更多相关文章

  1. poj 1995 裸快速幂

    1. poj 1995  Raising Modulo Numbers 2.链接:http://poj.org/problem?id=1995 3.总结:今天七夕,来发水题纪念一下...入ACM这个坑 ...

  2. POJ 1995 (快速幂)

    这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 #include <cstdio> #include <cmath> using namespace std ...

  3. 《挑战程序设计竞赛》2.6 数学问题-快速幂运算 POJ1995

    POJ3641 此题应归类为素数. POJ1995 http://poj.org/problem?id=1995 题意 求(A1^B1+A2^B2+ - +AH^BH)mod M. 思路 标准快速幂运 ...

  4. LibreOJ #6165. 一道水题

    二次联通门 : LibreOJ #6165. 一道水题 /* LibreOJ #6165. 一道水题 欧拉线性筛 其实题意就是求区间[1, n]所有数的最小公倍数 那么答案就是所有质因子最大幂次的乘积 ...

  5. ny525 一道水题

    一道水题时间限制:1000 ms  |  内存限制:65535 KB 难度:2描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他吗? ...

  6. NYOJ-525一道水题思路及详解

    一道水题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他 ...

  7. Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

    题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...

  8. 解题报告:poj 3070 - 矩阵快速幂简单应用

    2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...

  9. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

随机推荐

  1. K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  2. [转帖]Java 8新特性探究(九)跟OOM:Permgen说再见吧

    Java 8新特性探究(九)跟OOM:Permgen说再见吧 https://my.oschina.net/benhaile/blog/214159 need study 很多开发者都在其系统中见过“ ...

  3. (5.3)mysql高可用系列——mysql复制(理论篇)【续写中】

    关键词:mysql主从复制,mysql复制,MGR,mysql并行复制 目录 [1]mysql支持的复制类型 [2]mysql的主从4种同步方式介绍 (1)异步 (2)同步 (3)5.6 半同步 (4 ...

  4. 制作U盘的win7系统安装

    方法一 用iso.需要下载个UltraISO软件安装. 制作64位WIN7系统U盘安装盘方法 首页就有iso下载,有雨林木风等,我下载了系统之家最新的1907 U盘安装win7系统BIOS设置 thi ...

  5. 版本控制器之SVN(一)

    通常软件开发由多人协作开发,如果对代码文件.配置文件.文档等没有进行版本控制,将会出现很多问题: 备份多个版本,占用磁盘空间大 解决代码冲突困难 容易引发BUG 难于追溯问题代码的修改人和修改时间 难 ...

  6. CF407D Largest Submatrix 3

    cf luogu 被自己菜到自闭了/kk 既然是子矩阵,那么惯用套路为枚举矩阵上下边界,然后\(O(n)\)扫描求解.这题里要从左往右枚举右端点,然后看左端点最多能放到哪,那就对于每个数求出在上下边界 ...

  7. Hadoop环境安装和集群创建

    虚拟机使用vmware,vmware可以直接百度下载安装 秘钥也能百度到 安装很简单 CentOS 7下载: 进入官网 https://www.centos.org/download/ 这里有三种 第 ...

  8. Oracle创建表和创建序列和修改,增加sql字段

    一.创建表 create table y_parts_classify( pt_id number(10) not null, pt_name varchar2(1000), update_time ...

  9. 一个密码经过多次MD5加密能否提高安全性?Java MD5盐值加解密

    什么是MD5? MD5(Message Digest Algorithm 5,信息摘要算法5),是计算机广泛使用的摘要算法(又称哈希算法)之一.MD5是将一段信息,通过其不可逆的字符串变换算法,产生了 ...

  10. 华为ensp问题:云映射本地网卡,直连路由器可以ping通,pc却不行?

    拓扑图:cloud 云映射本机物理网卡:192.168.56.1     R1可以Ping通,所有Pc都不行,路由表也存在路由信息,不知道什么问题?