HDU4704 Sum

费马小定理

题目传送门

题目大意:给定一个数n,求把n分成1~n个数的方案数。
ans=ni=1Cin=2n1
因为n很大,而 (2,1e9+7)=1 因此可以考虑用费马小定理来降幂。
21e9+61(mod 1e9+7)
所以把幂 mod 1e9+6 ,把结果 mod 1e9+7 ,快速幂计算即可。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL MOD=1e9+6;
LL n;
inline char readc(){//因为读入时就要模掉,所以干脆写了个读优
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
inline LL _read(){
    LL num=0; char ch=readc();
    if (ch==EOF) return -1;//读到没有了就退出
    while (ch<'0'||ch>'9') { ch=readc(); if (ch==EOF) return -1;}
    while (ch>='0'&&ch<='9') { num=(num*10+ch-48)%MOD; ch=readc(); }//模掉
    return num;
}
LL qsm(LL a,LL b,LL MOD){//快速幂
    LL ans=1;
    while (b){
        if (b&1) ans=(ans*a)%MOD;
        b/=2;
        a=(a*a)%MOD;
    }
    return ans;
}
int main(){
    freopen("lj.in","r",stdin);
    freopen("lj.out","w",stdout);
    while (~(n=_read()))
        printf("%lld\n",qsm(2,(n-1+MOD)%MOD,MOD+1));
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值