HDU 1452 Happy 2004 (Fast Power modulo) __ number theory

Source: Internet
Author: User

For the 2004^x factor and modulo 29, sum (2004^X) represents the 2004^x factor and sum (2004^x) =sum (4^x*3^x*167^x), sum () is a integrable function,

Then there is sum (2004^x)%29=sum (4^x) *sum (3^x) *sum (167^x)%29

=sum (2^ (2*x)) *sum (3^x) *sum (167^x)%29=sum (2^ (2*x)) *sum (3^x) *sum (22^x)%29

Sum (p^ (X)) =1+p+p^2+......+p^n= (p^ (n+1)-1)/(P-1)

Then there are: Sum (2^ (2*x)) *sum (3^x) *sum (22^x)%29= (2^ (2*x)-1) * (3^ (x+1)-1)/2* (22^ (x+1)-1)/21)%29

Also: 15*2%29=1,21*18%29=1, then there are 15 2 modulo 29 of the multiplicative inverse, 18 is 21 modulo 29 of the multiplicative inverse

So you can get:

SUM (2004^x)%29= (2^ (2*x)-1) * (3^ (x+1)-1) *15* (22^ (x+1)-1) *18)%29

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
# Include<vector>

#define MM (a,b) memset (A,b,sizeof (a))
using namespace std;

const int INF=0X7FFFFFF;
Const double Pi=acos ( -1.0);
const double eps=1e-8;
const double e=2.7182818284590452354;

Computes the A^BMODN   
int modexp (int a,int b,int N)   
{   
    int ret=1;     
    while (b)   
    {    
       if (b&1) ret=ret*a%n;   
       A= (a*a)%n;   
       b>>=1;   
    }   
    return ret;   
}   
  
int main ()
{
	int n;
	while (~SCANF ("%d", &n), N)
	{
		int ans1=modexp (2,2*n+1,29);
		int Ans2=modexp (3,n+1,29);
		int Ans3=modexp (22,n+1,29);
		int ans= ((ans1-1) * (ans2-1) * (ans3-1) *15*18)%29;
		printf ("%d\n", ans);
	}
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.