zoukankan      html  css  js  c++  java
  • poj 1995 Raising Modulo Numbers(同余定理+快速幂)

    http://poj.org/problem?id=1995

    Raising Modulo Numbers
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 4430   Accepted: 2541

    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

    同余定理:(1)    (a+b) % m = (a % m + b % m) % m;证明:a = x*m + c, b = y*m = d; (a+b) = [ (x+y)*m +(a+b)]%m = (a+b)%m = (a % m + b % m) % m;

    (2) (x^y) % m = [(x%m)^y] % m;

    快速幂: 利用位运算,x^y,  如果y&1=1,则y的二进制最后一位是1,是奇数,否则偶数;y>>1, y的二进制数右移一位;

    http://baike.baidu.com/view/4533005.htm?fr=aladdin

    int pow3( int a, int b )
    {
        intr = 1, base = a;
        while( b != 0 )
        {
            if( b & 1 )
                r *= base;
            base *= base;
            b >>= 1;
        }
        returnr;
    }
    
    AC代码
    

    #include<stdio.h>
    __int64 pow(int x,int n,int m);
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	while(n--)
    	{
    		int k, a, b;
    		int i;
    		__int64 sum = 0, m;
    		scanf("%I64d %d",&m,&k);
    		for(i=0;i<k;i++)
    		{
    			scanf("%d %d",&a,&b);
    			sum += (pow(a%m,b,m) % m);
    			sum %= m;
    		}
    		printf("%I64d
    ",sum);
    	}
    	return 0;
    }
    
    __int64 pow( int a, int b,int m )
    {
        __int64 r = 1, base = a;
        while( b != 0 )
        {
            if( b & 1 )
    		{
                r *= base;
                r %= m;
            }
            base *= base;
            base %= m;
            b >>= 1;
        }
        return r;
    }
    
    


    www.cnblogs.com/tenlee
  • 相关阅读:
    MySQL 连接的使用:语法及案例剖析、INNER JOIN、LEFT JOIN、RIGHT JOIN
    MySQL GROUP BY 分组语句:语法及案例剖析、使用 WITH ROLLUP
    MySQL 排序:语法及案例剖析、在命令提示符中使用 ORDER BY 子句
    MySQL UNION 操作符:语法及案例剖析
    MySQL LIKE 子句:语法及案例剖析、在命令提示符中使用 LIKE 子句
    maven仓库
    maven安装配置
    Jedis
    Redis下载安装以及String类型
    Redis介绍
  • 原文地址:https://www.cnblogs.com/tenlee/p/4420156.html
Copyright © 2011-2022 走看看