HDU 4704 欧拉定理

题目看了很久没看懂

就是给你数n,一种函数S(k),S(k)代表把数n拆成k个数的不同方案数,注意如n=3,S(2)是算2种的,最后让你求S(1~n)的和模1e9+7,n<=1e100000。那么其实一个S(k)就是把n个小球放到k-1个盒子里的种类数,求和也就是求个$2^{n-1}$。

n超大,但是模数只有1e9+7,用欧拉定理就行了。

/** @Date    : 2017-09-12 18:41:59
  * @FileName: HDU 4704 欧拉定理 降幂.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const LL mod = 1e9 + 7;
const LL phi = 1e9 + 6;

char a[N];

LL fpow(LL a, LL n)
{
	LL res = 1;
	while(n)
	{
		if(n & 1)
			res = (res * a % mod + mod) % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

int main()
{
	while(~scanf("%s", a))
	{
		LL n = 0;
		for(int i = 0; i < strlen(a); i++)
		{
			n = ((n * 10LL) % phi + (LL)(a[i] - ‘0‘) ) % phi;
		}
		n = (n - 1 + phi) % phi;
		while(n < 0)
			n += phi;
		LL ans = fpow(2, n % phi + phi);
		printf("%lld\n", ans);
	}
    return 0;
}
时间: 2024-08-03 19:39:30

HDU 4704 欧拉定理的相关文章

HDU 4704 Sum( 费马小定理 )

HDU 4704 Sum( 费马小定理 ) 理解能力果然拙计,,题目看半天没懂什么意思. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MOD 1000000007 char str[100010]; LL fast_mod( LL a, int b) { LL res = 1; while( b )

hdu 4704 Sum (费马小定理+快速幂)

//(2^n-1)%mod //费马小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include <string.h> # define mod 1000000007 using namespace std; __int64 pow(__int64 n) { __int64 p=1,q=2; while(n) { if(n%

HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4704 Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 题意:给定一

hdu 4704 费马小定理+快速幂

题意就是:做整数拆分,答案是2^(n-1) 由费马小定理可得:2^n % p = 2^[ n % (p-1) ]  % p 当n为超大数时,对其每个数位的数分开来加权计算 当n为整型类型时,用快速幂的方法求解 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const in

HDU 4704 Sum (隔板原理 + 费马小定理)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4704 题意: 给定一个数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)

HDU 4704

http://acm.hdu.edu.cn/showproblem.php?pid=4704 求(2^n)%mod的方法 #include <iostream> #include <cstdio> #include <cstring> #include <set> #include <vector> #include <queue> using namespace std ; //(2^n)%mod=(2^(n%(mod-1)))%m

HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 题意是输入一个N,求N被分成1个数的结果+被分成2个数的结果+...+被分成N个数的结果,N很大 1.隔板原理 1~N有

hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                         (全题文末) 知识点: 整数n有种和分解方法. 费马小定理:p是质数,若p不能整除a,则 a^(p-1) ≡1(mod p).可利用费马小定理降素数幂. 当m为素数,(m必须是素数才能用费马小定理) a=2时.(a=2只是题中条件,a可以为其他值) mod m =  *      //  k=

hdu 4704 Sum (整数和分解+高速幂+费马小定理降幂)

题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7). 当中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                         (全题文末) 知识点: 整数n有种和分解方法. 费马小定理:p是质数,若p不能整除a.则 a^(p-1) ≡1(mod p). 可利用费马小定理降素数幂. 当m为素数,(m必须是素数才干用费马小定理) a=2时.(a=2仅仅是题中条件,a能够为其它值) mod m =   *      //