POJ 2478-Number theory training D problem Fermat theorem plus fast power

Source: Internet
Author: User

Description

Input

2

Output

2

        
 



This title means to give a number n, to divide it into 1 parts, 2 copies, 3 copies ... n copies (all of them are natural numbers) How many methods are there in total, and the data is very large and results in 1e9+7. Just beginning to see this problem a blank face, after the experience of learning predecessors, see some clues, after all, is related to mathematics, not to be seen. First, a list of n=1 to n=6 cases is found to satisfy an equation:
ans=2^ (n-1) mod (1e9+7), is coincidence or inevitable.
Originally this problem is high school probability contact, is a partition of the problem, said n something, put n-1 a partition is a question, get C (n-1,n-1), (c this symbol is a combination, the former in brackets is C of the subscript), put n-2 a partition, get C (n-1,n-2) ... Put a partition, that is, C (n-1,1), the combination of these combinations, is the two-item expansion, where the parentheses are the same (because 1 of any time 1). So the preceding equation is not accidental. Enclosed is a two-item theorem:
(a+b) ^n= (n from 0 to N) C (n,k) a^ (n-k) *b^k. Know what to count and how to go after. Input data is large, can only use string input, because it is the operation of Exponentiation, think of the fast power, just Chen teacher to the template, take to change into Longlong can be used. Considering that the power data is very large, we should use the Fermat theorem to power down.



Above is the Fermat theorem, the string to be recycled to take the remainder, with the rule of taking the remainder:
(A + b) mod n = ((a mod n) + (b mod n)) mod n;
(a–b) MoD n= ((a mod n)-(b mod n) + N) mod n;
AB MoD n= (a mod n) (b mod n) mod n;

1
2
3
4
5
6
7
8
9
30 of each of the above.
#include <iostream>
#include <string.h>
#define MOD 1000000007
using namespace std;
Long Long int quckpow (long long int m,long long int n,long long int k)
{
    long long int b=1;
    while (n>0)
    {
        if (n&1) b= (b*m)%k;
        n=n>>1;
        M= (m*m)%k;
    } return b;
}

int main ()
{

    long long int sum,ans;
    Char c[100005];
    while (cin>>c)
    {       sum=0;
        int L=strlen (c);
        for (int i=0;i<l;i++)
            sum= (sum*10+ (c[i]-' 0 '))% (mod-1);
        Ans=quckpow (2,sum% (mod-1) -1,mod);
        cout<<ans<<endl;
    }
    return 0;
}



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.