poj 1995 快速幂(裸)

Raising Modulo Numbers

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9218   Accepted: 5611

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>
using namespace std;

int pow1(int a,int b,int m)
{
    int res = 1;
    a %= m;
    while(b)
    {
        if(b&1)
            res = res*a%m;
        a = a*a%m;
        b >>= 1;
    }
    return res;
}

int main()
{
    int n;
    int mod,m,sum;
    int a,b;
    cin>>n;
    while(n--)
    {
        cin>>mod>>m;
        sum = 0;
        for(int i = 0;i<m;i++)
        {
            cin>>a>>b;
            sum = (sum + pow1(a,b,mod))%mod;
        }
        cout<<sum%mod<<endl;
    }
}

原文地址:https://www.cnblogs.com/ZZUGPY/p/8479607.html

时间: 2024-07-30 11:34:56

poj 1995 快速幂(裸)的相关文章

Raising Modulo Numbers(POJ 1995 快速幂)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: 3461 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

Raising Modulo Numbers POJ 1995(快速幂模板)

原题 题目链接 题目分析 快速幂模板题,依题意套个求模快速幂,然后答案边加边模即可. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include <ctime> 7 #include <cmath> 8 #include <cstring

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

Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1845 Appoint description:  System Crawler  (2015-05-27) Description Consider two natural numbers A and B. Let S be the sum of all natural d

Pseudoprime numbers POJ 3641(快速幂)

原题 题目链接 题目分析 依题意要先检测p是否为素数,这个可以用埃筛筛出1-sqrt(1e9)的素数,然后判定一下p是否能被这些数整除,不能的话就是素数,否则则为合数.至于a的p次方直接套个快速幂就行了. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include &

倍增法&amp;快速幂思想在解题中的应用

1.(a^b)mod p POJ 1995 快速幂板子题 (附带板子) typedef long long ll; ll mod; ll qp(ll a,ll b) { ll ret=1,base=a; while(b) { if(b&1) ret=(ret*base)%mod; base=(base*base)%mod; b=b>>1; } return ret; } 2.求(a*b)mod p; (a,b为longlong 64位整数乘法) 设b的二进制表示为b=Ck*2^k+Ck

POJ 1995 Raising Modulo Numbers(快速幂)

嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: 1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 int main(){ 7 long long N, M, n, a, b, c, sum = 0; 8 scanf("%lld", &N); 9 while(N--){ 10 scanf("%ll

poj Raising Modulo Numbers 快速幂模板

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8606   Accepted: 5253 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

dutacm.club Water Problem(矩阵快速幂)

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

矩阵快速幂专题(一)

最近闲来无事,准备集中精力刷一波数论与图论.矩阵快速幂是数论里面的重要组成部分,值得我好好学习一下.因为题目比较多,分析也比较多,所以将此专题分成几个部分.做完这一专题,可能会暂时转向图论部分,然后等我组合数学学得差不多了,再回过头来继续做数论题. 矩阵快速幂算法的核心思想是将问题建模转化为数学模型(有一些简单题目是裸的矩阵模型,但是大部分难题就是难在要构造矩阵,用矩阵方法解决问题),推倒递推式,构造计算矩阵,用快速幂的思想求解矩阵A的n次方取mod,从而得到矩阵里面你需要的数据. 矩阵快速幂问