Raising Modulo Numbers_快速幂取模算法

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

【题意】给出h组a,b的值,求a1^b1+a2^b2+...+an^bn之和mod p的值

【思路】快速幂二进制取模算法

参考资料:http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html

http://blog.csdn.net/zhangv123/article/details/47953221

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int fun(long long int a,long long int b,long long int p)
{
    long long int res=1;
    while(b)
    {
        if(b&1) res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
int main()
{
    long long int t;
    long long int a,b,h,p;
    scanf("%lld",&t);
    while(t--)
    {
        long long int ans=0;
        scanf("%lld%lld",&p,&h);
        for(int i=1;i<=h;i++)
        {
            scanf("%lld%lld",&a,&b);
            ans=ans+fun(a,b,p);

        }
        ans=ans%p;
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-08-02 15:13:44

Raising Modulo Numbers_快速幂取模算法的相关文章

【转】C语言快速幂取模算法小结

(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余).在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法.我们先从简单的例子入手:求abmodc 算法1.直接设计这个算法: int ans = 1; for(int i =

快速幂取模算法

什么是快速幂? 快速幂应当是快速幂取模的简称 对于一般的求幂算法,求$a^b\,\bmod\,m$,即使用循环b次的方法,复杂度是$O(b)$的,当b很大的时候,这种算法就会显得十分缓慢. 快速幂是基于以下明显的事实: $${a^b} \equiv {(a^2)^{\frac{b}{2}}} \pmod{m}\quad b\ is\ even$$ $${a^b} \equiv {(a^2)^{\frac{b}{2}}*a} \pmod{m}\quad b\ is\ odd$$ 那么我们得到这样一

快速幂取模算法【模板】

快速幂取模其实是a^b%c,这就是著名的RSA公钥加密的方法,当a,b都很大的时候,直接求是不可取的,所以就用到了快速幂取模. 首先你得明白他的原理,其实是用到了二分的思想,把b按照二进制展开 b = p(n)*2^n  +  p(n-1)*2^(n-1)  +-+   p(1)*2  +  p(0).其中p(i) (0<=i<=n)为 0 或 1. 所以此时a^b =  a^ (p(n)*2^n  +  p(n-1)*2^(n-1)  +...+  p(1)*2  +  p(0))=  a^

POJ1995:Raising Modulo Numbers(快速幂取余)

题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { int

快速幂取余算法

下面是一个快速幂的介绍: 先贴一个秦九韶算法(Horner算法)的原理: 设有项的次函数 将前项提取公因子,得 再将括号内的前项提取公因子,得 如此反复提取公因子,最后将函数化为 令 ...... 则即为所求 下面是讲解快速幂的:(By  夜せ︱深   感谢作者) 快速幂取模算法 在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~ 所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求

快速幂取模和快乘取模

一.快速幂取模概念 快速幂取模,顾名思义,就是快速的求一个幂式的模(余),比如a^b%c,快速的计算出这个式子的值. 在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法. 二.快速幂取模算法实现 1)很容易能想到,循环b次,每次乘a,最后对c取余就可以了. int ans = 1; for(int i = 1; i<=b; i++) { ans = ans * a; } ans = ans % c; 这个朴素算法的问题是: 1.如果a和b

快速幂取模(POJ 1995)

http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c=((a%c)*b)%c 每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 由此可以用快速幂算法优化: http://www.cnblogs.com/qlky/p/5020402.html 再结合取模公式: (a + b) % p = (a % p

快速幂及快速幂取模

快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log?N), 与朴素的O(N)相比效率有了极大的提高.——bybaidu 快速幂可以用位运算这个强大的工具实现. 代码: 1 int pow(int a,int b) 2 { 3 int ans=1; 4 while(b!=0) 5 { 6 if(b&1) 7 ans*=a; 8 a*=a; 9 b>>=1; 10 } 11 return ans; 12 } 快速幂取模需要记住一个定理:积的取模等于取模积的取模:算法是蒙

关于快速幂取模

今天看算法书的时候,看到一道关于快速幂取模的题,心想好像不难,便写了一下,发现我的渣渣代码写的比正常的O(N)复杂度还要慢(天知道我怎么做到的T_T),渣渣代码如下: 1 public static long fastMi(long x,long n){ 2 if(n==1){ 3 return x; 4 } 5 if(n%2==0){ 6 return fastMi(x,n/2)*fastMi(x,n/2); 7 }else{ 8 return fastMi(x,n/2)*fastMi(x,n