Happy 2004 (Fast Power + multiply inverse)

Source: Internet
Author: User

Happy 2004

Problem 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:

1100000

Sample output:

610

Set S (x) to represent the factor of X and. The title is: S (2004^x) MoD 29
The factor and s are an integrable function, that is, satisfies the nature of 1.

Property 1: If GCD (A, b) =1 then S (a*b) = S (a) *s (b)
2004^x=4^x * 3^x *167^x
S (2004^x) =s (2^ (2X)) * S (3^x) * S (167^x)

Property 2: If P is a prime number then S (p^x) =1+p+p^2+...+p^x = (p^ (x+1)-1)/(P-1)
Therefore: S (2004^x) = (2^ (2x+1)-1) * (3^ (x+1)-1)/2 * (167^ (x+1)-1)/166
167%29 = = 22
S (2004^x) = (2^ (2x+1)-1) * (3^ (x+1)-1)/2 * (22^ (x+1)-1)/21

Property 3: (a*b)/C%m= a%m * b%m * INV (c)
where INV (c) satisfies the smallest integer (C*INV (c))%m=1, where m=29
Then INV (1) =1,INV (2) =15,INV (22) =15

There are:
S (2004^x) = (2^ (2x+1)-1) * (3^ (x+1)-1)/2 * (22^ (x+1)-1)/21
= (2^ (2x+1)-1) * (3^ (x+1)-1) *15 * (22^ (x+1)-1) *18

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8#include <Set>9 using namespacestd;Ten #defineLL Long Long One Const intinf=0x3f3f3f3f; A Const Doubleeps=1e-5; - intp= in; - ll Pow_mod (ll x,ll N) the { -LL res=1; -      while(n>0) -     { +         if(n&1) res=res*x%p; -x=x*x%p; +n>>=1; A     } at     returnRes; - } - intMain () - { - LL x,i; -      while(cin>>x&&x) in     { -         intA=pow_mod (2,2*x+1); to         intB=pow_mod (3, x+1); +         intC=pow_mod ( A, x+1); -         intS= (A-1) * (b-1) * (C-1)* the* -)% in; thecout<<s<<Endl; *      } $ Panax Notoginseng}

Happy 2004 (Fast Power + multiply inverse)

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.