Elementary theory (Fermat theorem)-Happy 2004

Source: Internet
Author: User

Description

Consider a positive integer x,and let S is the sum of all positive integer divisors of 2004^x. Your job is to determine s modulo (the rest of the division of S by 29).

Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6,, 167, 334, 501, 668, 1002 and 2004. Therefore s = 4704 and s modulo are equal to 6.
Input

The input consists of several test cases. Each test case is contains a line with the integer x (1 <= x <= 10000000).

A test Case of X = 0 indicates the end of input, and should not being processed.
Output

For each test case, in a separate line, please output the result of S modulo 29.
Sample Input

1
10000
0
Sample Output

6
10

-----------------------------------------------------------------I'm the split line ^_^-------------------------------------- ------------------------------------------

The title of the problem is quite happy, do the problem I do not happy, I have been thinking about what method, after all, water is too deep, I also drowned =
The old look, first explain the title: the title of the topic is given an X, the requirement to find out 2004 of the X-square factor and, factor and well, such as the 4 factor and is
1 + 2 + 4 = 7, and then, the title of the meaning is like this, and then I am a little more than a long time to find out the problem, the search is still ignorant, what the devil,
Can not be said in detail, the details of the typing is not accurate, do not understand the part there are so many ... Forget about the groove.

First, a science: Fermat theorem,
The Fermat theorem (Fermat theory) is an important theorem in number theory, which includes: if P is prime, and gcd (a,p) = 1, then a^ (p-1) ≡1 (mod p).
That is: If A is an integer, p is a prime number, and A,p coprime (that is, there is only one convention of 1), then the remainder of a (p-1) divided by P is constant equal to 1.

Combined with the same Yude theorem, a^ (p-1)%p = 1%p = 1, that is, in some cases 1 can be replaced with the first thing, wait a bit useful.

Look at the topic of course no clue, directly to the method, we first put 2004 decomposition factorization, got 2, 3,167, what is the use of these things? Let's take a look at a new thing.


The factor of a number and is an integrable function
About the integrable function, i.e. F (AB) =f (a) *f (b), there are many integrable functions in number theory

To prove it:

S (x) represents the factor of X and.

If x can be divided into a, B (which must be a prime number), then S (x) =s (a) *s (b).

Why must be divided into prime, because a prime factor after 1 and its own, for a, B, is 1,a,1,b, then the x=a*b,x factor is only 1,a, b,x these four numbers,

This is called a number factor and is an integrable function.

The title is: S (2004^x) MoD 29

So you know: 2004=4 * 3 *167 (Note that 4 is not prime, but the use requirement must be prime, so replace 2 squared)
S (2004^x) =s (2^ (2X)) * S (3^x) * S (167^x)

If P is a prime number of its factor is only 1 and its own, so in order to find its factor and the time can use geometric series summation formula, do not know how to use dislocation phase subtraction to revisit the high school knowledge = =,

So there are: S (p^x) =1+p+p^2+...+p^x = (p^ (x+1)-1)/(p-1), this expression means to ask for the X-dimension of P's factor and

So: S (2004^x)% = (2^ (2x+1)-1)% * (3^ (x+1)-1)/2 * (167^ (x+1)-1)/166 29,

Because the answer is to 29 modulo, so the previous one can be used to calculate the fast power modulo, but the latter two with division is a bit difficult to do,

In multiplication can be a*b%c = (a%c * b%c)%c, addition can also have (a+b+c)%c = (A + (b+c)%c)%c, subtraction is also the same, and the same as the addition,

Unfortunately, division is a wonderful, it is non-mainstream =, so can not do so, do not believe that you calculate (14/2)%4, you replace (14%4)/(2%4) answer

is not the same, this time is going to use a kind of something called inverse, its role is to change the division into a multiplication modulo, such as A/b% c can be changed to a * b^ (c-2)%c,

where b^ (c-2) is the inverse of B, which is only a solution to the inverse, because of the combination of Fermat theorem, this method is more suitable for the computer, because it can be used fast power

Modulo to solve the operation, now to start the change: A/b% c = A * b^ ( -1)% c = A * b^ ( -1) * 1 C, and then back to see the above Fermat theorem below

That little hint, you can replace the 1, the result becomes a * b^ ( -1) * b^ (c-1)% c = A * b^ (c-2)% c, OK, so that a smooth completion of the conversion,

The modulo of division also becomes multiplication, next can use the fast power to take the model to carry on the massacre, but when writes the code the time to pay attention to reduce one (geometric series summation) ....

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<map>UsingNamespace Std;IntMod(int a,int b,int C){//Fast power modulo calculates the result of A's B-th square mode Cint ans=1;While(b){If(b&1){ans= ans) B% c;} b>>=1; A= A) B% c;}return ans;}IntMain()//If you look at N is not used to, according to the above habits, the N to X on the line{Freopen ("Input.txt", "R", stdin);int n;While(scanf("%d",&n), n){int a=Mod(2,2* N+1,29)-1;//Seeking S (a) int b=(Mod(3N+1,29)-1)*Mod(2,27,29);//Ask S (b) int C=(Mod(22N+1,29)-1 * mod (212729 Printf ( "%d\n  "* b * C % 29);//Here is the answer, s (answer) = S (a) * s (b) * s (c), I don't understand. singularity function = = } return 0;               /span>          




Elementary theory (Fermat theorem)-Happy 2004

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.