Hdu4152-happy 2004-integrable function and fast power multiplication

Source: Internet
Author: User

Topic Link http://acm.hdu.edu.cn/showproblem.php?pid=1452

The main subject is the application of integrable function and fast power multiplication.

The following is a brief description of the integrand function:

An integrable function is a relationship that describes a factor and:


The factor of 6 is 1,2,3,6; The factor of 6 and is S (6) =1+2+3+6=12;

The factor of 20 is 1,2,4,5,10,20; The factor of 20 and is S (20) =1+2+4+5+10+20=42;

The factor of 2 is 1, 2; The factor of 2 and is S (2) =1+2=3;

The factor of 3 is 1, 3; The factor of 3 and is S (3) =1+3=4;

The factor of 4 and is S (4) =1+2+4=7;

The factor of 5 and is S (5) =1+5=6;

S (6) =s (2) *s (3) =3*4=12;

S (=s) (4) *s (5) =7*6=42;

Look again s () = 1+2+5+10+25+50=93=3*31=s (2) *s (+), S (25) =1+5+25=31.

This is called the integrable function in number theory.

When the GCD (A, b) = 1 o'clock S (a*b) =s (a) *s (b)


If P is a prime number


S (p^n) =1+p+p^2+...+p^n= (p^ (n+1)-1)/(p-1) (1)


The subject to calculate is: s (2004^x) MoD 29,

2004=2^2 *167

S (2004^x)) = (s (2^2x))) * (S (3^x))) * (S (167^x)))


167%29=22


S (2004^x)) = (s (2^2x))) * (S (3^x))) * (S (22^x)))


A=s (2^2x) = (2^ (2x+1)-1)% //According to (1)
B=s (3^x) = (3^ (x+1)-1)/2 //According to (1)
C=s (22^x) = (22^ (x+1)-1)/21 //According to (1)


(A/b)%p= (a *b^ ( -1)%p) (2)


b^ (-1) is the inverse element of B

The inverse element of 1 is 1;
The inverse element of 2 is 15, because 2*15=30% 29=1% 29=1;
The inverse element of 21 is 18, because 21*18=378% 29 = 1 29=1;
So:
A= (POW (2,2*x+1,29)-1);//According to (2)
b= (POW (3,x+1,29)-1) *15; // based on (2)
C= (POW (22,x+1,29)-1) *18; // based on (2)

That is, the value of the POW fast Power multiplier is calculated below.

int pow (int a,int b)
{
int sum=1;
while (b)
{
if (b&1)
Sum=sum*a%29;
b>>=1;
A=a*a%29;
}
return sum;

}

Attached code:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int pow (int a,int b) {int Sum=1;while (b) {if (b&1)  Sum=sum*a%29;b>>=1;a=a*a%29;} return sum;} int main () {int n;while (~scanf ("%d", &n), n) {int a,b,c;a= (POW (2,2*n+1)-1);        <span style= "White-space:pre" ></span>b= (POW (3,n+1)-1) *15;       <span style= "White-space:pre" ></span>c= (POW (22,n+1)-1) *18;        

Reprint please indicate the source, otherwise investigate its legal responsibility!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu4152-happy 2004-integrable function and fast power multiplication

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.