HDU 4704 Sum (partition principle + Fermat theorem)

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=4704


Test instructions

Given a number n to decompose it, Si represents the number of schemes to split n into the number of I

Seek sum (SI) 1<=i<=n;


Analysis:

Partition principle, n a stick, n-1 a seam,

Divided into 1 parts is C (n-1,0);

Divided into 2 parts is C (n-1,1);

Divided into 3 parts is C (n-1,2);

...

Divided into n parts is C (n-1,n-1);

ans = SUM (C (n-1,i)) (0<=i<=n-1)

=2^ (n-1);

Due to modulo and 2 and mod coprime, you can use Fermat theorem to power down


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;typedef long Long ll;const int mod = 1e9+7;const int maxn = 1e5+10;char A[MAXN]; ll Quick_mod (ll a,ll b,ll m) {    ll ans = 1;    while (b) {        if (b&1) {            ans = ans * a% m;            b--;        }        b>>=1;        A = A * a% m;    }    return ans;} ll Change (char *s, ll m) {    ll ans = 0;    int len = strlen (s);    for (int i = 0; i < len; i++) {        ans = (ans * + a[i]-' 0 ')%m;    }    return ans;} int main () {    while (~scanf ("%s", a)) {        int m = mod-1;        LL n = Change (a,m);        printf ("%i64d\n", Quick_mod (2, (n-1+m)%m,mod));    }    return 0;}






HDU 4704 Sum (partition principle + Fermat theorem)

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.