HDU 4704 Sum (integer and decomposition + fast Power + Fermat theorem Power down)

Source: Internet
Author: User

Test instructions

Give N (1<n<), (S1+S2+S3+...+SN) mod (1e9+7).                         Where Si represents a number of n by the number of I added, such as n=4, then s1=1,s2=3. (End of whole text)

Knowledge Points:

Integer n the kind and decomposition method.

Fermat theorem: P is prime, if p cannot divide a, then a^ (p-1) ≡1 (mod p). The Ma Xiao theorem can be used to reduce the power of primes.

When M is a prime number, (M must be a prime number to use the Fermat theorem)

When a=2. (a=2 is only the condition in question, a can be other values)

MoD m = *//k=

=//==1 for the application of Fermat theorem

For example, set p=7, n=32, ask for 2^32≡x (mod p) value

Because P is a prime number, there must be 2^6≡1 (mod p)

The

2^32%p= (2^[(6*5) +2])%p

=[2^ (6*5) *2^2]%p

=[(2^ (6*5)%p) * (2^2%p)]%p//(A*B)%m=[(a%m) * (b%m)]%m;

=[1* (2^2%p)]%p//2^ (6*5)%p for the application of Fermat theorem

=2^2%p;

Exercises

The problem is equal to the number of decomposition of N. For example, n=x1+x2+x3+. XK is a decomposition that regards XI as being made up of Xi 1, and N is the N 1 component.

The topic is the number of methods that give N 1 groupings (this is not a ball spacer problem similar to combinatorial math). Every two 1 whether to put a baffle, there are put and not two options, altogether n-1 an optional interval. So the total number of methods is.

Because N is too big, not good processing ah.

If the exponent is too large and the m=1e9+7 is found to be a prime number, the Fermat theorem (a^ (p-1)) ≡1 (mod p) can be used to power down.

#include <iostream> #include <cstdio> #include <cstring>using namespace std;typedef long Long ll;const int Mod=1e9+7,n=1e5+5;char A[n]; ll Quick_mod (ll a,ll P)          //Fast power (fast power using two points thought and Qin algorithm) {    ll ans=1;    while (p)    {        if (p&1)            ans=ans*a%mod;        A=a*a%mod;        p>>=1;    }    return ans;} int main () {    while (~scanf ("%s", a))    {        int len=strlen (a);        LL ans=0;        for (int i=0;i<len;i++)        {            ans= (ans*10+a[i]-' 0 ')% (mod-1);        }        ans= (ans-1+mod-1)% (mod-1);        printf ("%lld\n", Quick_mod (2,ans));    }    return 0;}

This problem can also find a loop knot.

Found 2^500000003 = 1 = 2^0, so n= (n-1)%500000003, so 2^ (n-1) = 2^ ((n-1)% (mod-1))%mod; (mod-1=500000003)

Sum

Time limit:1000MS Memory Limit:131072KB 64bit IO Format:%i64d &%i64u

Submit Status

Description

Sample Input

Sample Output

Hint

HDU 4704 Sum (integer and decomposition + fast Power + Fermat theorem Power down)

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.