Description
Input
2
Output
2Hint1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases.给定一个数n 将其分解,Si 表示将n拆成i个数的方案数
求sum( si ) 1<=i<=n;
分析:
隔板原理, n个木棍,n-1个缝,
分成1份则是C(n-1,0);
分成2份则是C(n-1,1);
分成3份则是C(n-1,2);
...
分成n份则是C(n-1,n-1);
ans = sum( C(n-1,i) ) (0<=i<=n-1)
=2^(n-1);
由于要取模而且 2与 mod 互质,因此可以用费马小定理来降幂
题目要求s1+s2+s3+...+sn;//si表示n划分i个数的n的划分的个数,如n=4,则s1=1,s2=3
假设An=s1+s2+s3+...+sn;
对于n可以先划分第一个数为n,n-1,n-2,...,1,则容易得出An=A0+A1+A2+A3+...+A(n-1);
=>A(n+1)=A0+A1+A2+A3+...+An =>An=2^(n-1);
由于n非常大,所以这里要用到费马小定理:a^(p-1)%p == 1%p == 1;//p为素数
所以2^n%m == ( 2^(n%(m-1))*2^(n/(m-1)*(m-1)) )%m ==(2^(n%(m-1)))%m * ((2^k)^(m-1))%m == (2^(n%(m-1)))%m;//k=n/(m-1)
2^n%m=(2^(n%(m-1)))%m
然后快速幂
费马小定理(Fermat Theory)是数论中的一个重要定理,其内容为: 假如p是质数,且Gcd(a,p)=1,那么 a(p-1) ≡1(mod p)。即:假如a是整数,p是质数,且a,p 互质 (即两者只有一个 公约数 1),那么a的(p-1)次方除以p的余数恒等于1。该定理是1636年 皮埃尔·德·费马 发现的。#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int N = 1000000; char str[N]; const int mod = 1000000007; typedef long long LL; LL quick(LL x, LL n); int main() { while(scanf("%s", str)!=EOF) { LL n=0; for(int i=0;str[i];i++) { n=(n*10+(str[i]-'0'))%(mod-1); } printf("%I64d\n",quick(2,n-1)%mod); } return 0; } LL quick(LL x, LL n) { LL r=1; while(n!=0) { if(n&1) { r=(r*x)%mod; } x=(x*x)%mod; n>>=1; } return r; }