POJ 1845 Sumdiv (arithmetic basic theorem to find a number factor and)

Source: Internet
Author: User
Tags cmath

To find all the factors of a number and to use the basic theorem of arithmetic, here are two important applications:
(1) A positive integer greater than 1 N, if its standard decomposition is: n= (P1^A1) * (P2^A2) ... (Pn^an)
Then it's a number of numbers (1+A1) (1+A2) ..... (1+an).
(2) The sum of its all positive factors is d (N) = (1+p1+...p1^an) (1+P2+...P2^A2) ... (1+pn+...+pn^an)
And the method of finding a number is analogous to a number of numbers.
It is possible to first make a list of all the primes within sqrt (n) (and certainly not the table), since only one of the prime factors of n is greater than sqrt (n) (as evidenced in the previous topic),
So it can be processed at the end. For each item is a geometric series, summation is easy.


The following is an example of POJ 1845 Sumdiv.
The n^m%p,p is the prime number 9901.
1) because N and M are large, you can handle all the factor of N, then ^m the exponent of each factor *m. This problem can not be preprocessed all prime numbers, directly decomposition factor, for the time complexity of the problem is acceptable;
2) Use the fast power operation.

3) When seeking geometric series and modulus of two methods: 1. Summation formula with geometric series:s=a1* (q^n-1)/(q-1), to use the inverse of (q-1) modulo 9901, can be obtained by Euler theorem or extended Euclidean, but because the mod is a prime number, So the inverse of a is a^ (mod-2)%mod, with the fast power to seek. 2. Recursive form of dichotomy


for inverse complement: The Fermat theorem (Fermat theory) is an important theorem in number theory, with the following content: 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.


Inverse element method for solving

#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath > #define MAXN 10000#define ll long longusing namespace Std;const ll mod=9901;ll prime[maxn],num,flag[maxn+5];ll Pow_mo    D (ll a,ll b,ll p) {//fast power ll ret=1;        while (b) {if (b&1) ret= (ret*a)%p;        A= (a*a)%p;    b>>=1; } return ret;}    void Get_prime () {//Sieve prime memset (flag,0,sizeof (flag));    num=0;        for (ll i=2;i<maxn;i++) {if (!flag[i]) prime[++num]=i;            for (ll j=1;j<=num&&i*prime[j]<maxn;j++) {flag[i*prime[j]]=1;        if (i%prime[j]==0) break;    }}}ll get_sum (ll k,ll N) {//calculation (1+k+k^2+...+k^n)%mod, geometric series and for (k^ (n+1)-1)/(K-1);    if (n==0) return 1;    ll Ret=1;    Ret=ret* (Pow_mod (K,n+1,mod)-1)%mod;       Ret=ret*pow_mod (k-1,mod-2,mod)%mod; (k-1) of the inverse, to note (k-1)%9901! = 0 return ret;}    int main () {ll n,m,k,a;    Get_prime (); while (~SCANF ("%lld%lld", &n,&m)) {ll ans= 1;     if (!n&&m) ans=0;        0^0=1, 0^1..=0;            The factor decomposition for (int i=1;i<=num&&prime[i]*prime[i]<=n;i++) {if (n<prime[i]) breaks;               if (n%prime[i]==0) {a=0;                    exponential while (n%prime[i]==0) {n/=prime[i];                a++;                } a*=m;                K=prime[i];            Ans=ans*get_sum (k,a)%mod; }}//If there is also a vegetarian factor greater than sqrt (n), its exponent must be 1*m=m if (n>1&& (n-1)%mod==0) ans=ans* (Pow_mod (n,m+1,mod* (n   -1))/(n-1))%mod;        if (n-1)%mod==0, cannot seek inverse meta-if (n>1) ans=ans*get_sum (n,m)%mod;                printf ("%lld\n", ans); I didn't add mod wa several times at first, because this method ans may be negative} return 0;}


The solution of the dichotomy method

#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath > #define MAXN 10000#define ll long longusing namespace Std;const ll mod=9901;ll prime[maxn],num,flag[maxn+5];ll Pow_mo    D (ll a,ll b,ll p) {//fast power ll ret=1;        while (b) {if (b&1) ret= (ret*a)%p;        A= (a*a)%p;    b>>=1; } return ret;}    void Get_prime () {//Sieve prime memset (flag,0,sizeof (flag));    num=0;        for (ll i=2;i<maxn;i++) {if (!flag[i]) prime[++num]=i;            for (ll j=1;j<=num&&i*prime[j]<maxn;j++accepted) {flag[i*prime[j]]=1;        if (i%prime[j]==0) break;    }}}ll get_sum (ll k,ll N) {//Can be computed with a recursive binary (1+k+k^2+...+k^n)%mod if (n==0) return 1;    if (n&1) return ((1+pow_mod (K,n/2+1,mod)) *get_sum (K,N/2))%mod; else Return ((1+pow_mod (K,n/2+1,mod)) *get_sum (k,n/2-1) +pow_mod (k,n/2,mod))%mod;}    int main () {ll n,m,k,a;    Get_prime (); while (~SCANF ("%lld%lld", &n,&m) {ll ans=1; if (!n&&m) ans=0;        0^0=1, 0^1..=0;            for (int i=1;i<=num&&prime[i]*prime[i]<=n;i++) {if (n<prime[i]) break;               if (n%prime[i]==0) {a=0;                    exponential while (n%prime[i]==0) {n/=prime[i];                a++;                } a*=m;                K=prime[i];            Ans=ans*get_sum (k,a)%mod;        }} if (n>1) ans=ans*get_sum (n,m)%mod;    printf ("%lld\n", ans); } return 0;}




POJ 1845 Sumdiv (arithmetic basic theorem to find a number factor and)

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.