Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 589    Accepted Submission(s): 292

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.

 
/*分析:题目要求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)
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=100000+10;
const int mod=1000000000+7;
char s[MAX]; __int64 MOD(char *a,int Mod){
__int64 sum=0;
for(int i=0;a[i] != '\0';++i){
sum=(sum*10+a[i]-'0')%Mod;
}
return sum;
} __int64 FastPow(__int64 a,__int64 k){
k=(k+mod)%mod;
__int64 sum=1;
while(k){
if(k&1)sum=sum*a%mod;
a=a*a%mod;
k>>=1;
}
return sum;
} int main(){
while(scanf("%s",s)!=EOF){
__int64 n=MOD(s,mod-1)-1;
printf("%I64d\n",FastPow(2,n));
}
return 0;
}

hdu4704之费马小定理+整数快速幂的更多相关文章

  1. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  2. hdu 4704 Sum(组合,费马小定理,快速幂)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4704: 这个题很刁是不是,一点都不6,为什么数据范围要开这么大,把我吓哭了,我kao......说笑的, ...

  3. bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

    题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...

  4. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

  5. HDOJ 5667 Sequence//费马小定理 矩阵快速幂

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意:如题给了一个函数式,给你a,b,c,n,p的值,叫你求f(n)%p的值 思路:先对函数取以a为 ...

  6. 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

    [题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...

  7. HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂

    MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3) mod 1000000007 是质数 , 依据费马小定理  a^phi( p ) = 1 ( ...

  8. M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. hdu_4869(费马小定理+快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 Turn the pokers Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. Delphi 的动态数组

    传统的Pascal 语言其数组大小是预先确定的,当你用数组结构声明数据类型时,你必须指定数组元素的个数.专业程序员也许知道些许动态数组的实现技术,一般是采用指针,用手工分配并释放所需的内存. Delp ...

  2. MySQl 存储过程+游标

    DROP PROCEDURE IF exists pro_Insertflightplan_stat;  create procedure pro_Insertflightplan_stat(exec ...

  3. 修改项目工程名 iOS

    结合这两篇原文: http://www.cocoachina.com/ios/20150104/10824.html http://jingyan.baidu.com/article/0964eca2 ...

  4. Cramfs、JFFS2、YAFFS2的全面对比

    Cramfs.JFFS2.YAFFS2的全面对比http://blog.csdn.net/daofengdeba/article/details/7721340 由于嵌入式系统自身存在一些特殊要求,使 ...

  5. centos下卸载jdk

    链接地址:http://blog.csdn.net/shuixin536/article/details/8954011 http://sunqiusong.email.blog.163.com/bl ...

  6. HTML5 总结-表单-输入类型

    HTML5 Input 类型 HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: email url nu ...

  7. PHP 导入excel

    db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  PHP代码: header("Content-type:text/html; ...

  8. 写一个兼容性比较好的拖拽DEMO

    写一个兼容性比较好的拖拽DEMO 查看Demo 思路 div盒子 鼠标按下事件onmousedown 鼠标移动事件onmousemove,获得鼠标的坐标,将div移动至鼠标的当前坐标 鼠标抬起事件om ...

  9. What is Webhook ( Introduction to Webhook )

    A webhook in web development is a method of augmenting or altering the behavior of a web page, or we ...

  10. Windows Phone 8初学者开发—第7部分:本地化应用程序

    原文 Windows Phone 8初学者开发—第7部分:本地化应用程序 第7部分:本地化应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phon ...