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

输入 n 和 k

首先 f(n)中k的个数 等于 f(n-1) 中 k-1的个数

最终等于 f(n-k+1) 中 1 的个数

舍 s(n) = f(n) + f(n-1) + ....+ f(1)

则 f(n) = s(n) - s(n-1)

由于 s(n) = s(n-1) + 2^(n-2) + s(n-1) = 2*(s(n-1)) + 2^(n-2)

= 2^(n-1) + (n-1)*2^(n-2)

       = (n+1)*2^(n-2)

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<stdexcept>
#include<bitset>
#include<cassert>
#include<deque>
#include<numeric> using namespace std; typedef long long ll;
typedef unsigned int uint;
const double eps=1e-12;
const int INF=0x3f3f3f3f;
const ll MOD=1000000007;
ll power(ll x,ll y)
{
ll tmp=1;
while(y)
{
if((y&1))
tmp=(tmp*x)%MOD; x=(x*x)%MOD;
y=y>>1;
}
return tmp;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
cin>>T;
while(T--)
{
ll n,m;
cin>>n>>m;
ll k=n-m+1;
if(k<=0)
{
cout<<"0"<<endl;
continue;
}
if(k==1)
{
cout<<"1"<<endl;
continue;
}
if(k==2)
{
cout<<"2"<<endl;
continue;
}
ll w1=(k+1)*power(2,k-2)%MOD;
--k;
ll w2=(k+1)*power(2,k-2)%MOD;
cout<<(w1-w2+MOD)%MOD<<endl;
}
return 0;
}

hdu 4602 Partition的更多相关文章

  1. hdu 4602 Partition 数学(组合-隔板法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 我们可以特判出n<= k的情况. 对于1<= k<n,我们可以等效为n个点排成 ...

  2. hdu 4602 Partition (概率方法)

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDU 4602 Partition (矩阵乘法)

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. hdu 4602 Partition 矩阵快速幂

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  5. hdu 4602 Partition(快速幂)

    推公式+快速幂 公式有很多形式,可以写矩阵 1.前n-1项和的两倍+2的(n-2)次方,这个写不出啥 2.递推式:f(n)=2*f(n-1)+2的(n-3)次方 3.公式:2的(n-k-2)次方*(n ...

  6. hdu 4602 Partition(矩阵快速幂乘法)

    Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...

  7. 【HDU 4602】Partition

    题意 给你一个数n,把它写成几个正整数相加的形式,即把n拆开成若干段,把所有可能的式子里正整数 k 出现的次数取模是多少. 分析 特判 k>=n 的情况. k<n时:问题相当于n个点排一行 ...

  8. Partition HDU - 4602 (不知道为什么被放在了FFT的题单里)

    题目链接:Vjudge 传送门 相当于把nnn个点分隔为若干块,求所有方案中大小为kkk的块数量 我们把大小为kkk的块,即使在同一种分隔方案中的块 单独考虑,它可能出现的位置是在nnn个点的首.尾. ...

  9. HDU 4651 Partition 整数划分,可重复情况

    Partition Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. [转载] zookeeper 事件通知

    ZK事件回调当一个client访问ZK时,client与ZK保持长连接.应用可以通过client的api注册一些callback,当对应的事件发生时,client会执行对应的callback.如果你基 ...

  2. 事件冒泡与事件委托 -Tom

    事件冒泡 事件冒泡,就是事件触发的时候通过DOM向上冒泡,首先要知道不是所有的事件都有冒泡.事件在一个目标元素上触发的时候,该事件将触发祖先节点元素,直到最顶层的元素: 如图所示,如果a连接被点击,触 ...

  3. Target runtime Apache Tomcat v7.0 is not defined.

    打开项目,找到.settings--->org.eclipse.wst.common.project.facet.core 修改这个文件中: <?xml version="1.0 ...

  4. html5实现GIF动画!

     代码如下: <!DOCTYPE html><html>    <head>        <meta charset="utf-8"&g ...

  5. Manacher 算法(hdu 3068 && hdu 3294)

    今天打算补前晚 BC 的第二题,发现要用到能在 O(n) 时间求最大回文子串长度的 Manacher 算法,第一次听,于是便去百度了下,看了大半天,总算能看懂了其思想,至于他给出的代码模板我没能完全看 ...

  6. IE6 + png24 透明图片(滤镜)

    IE6 + png24 透明图片(滤镜) .IE6png{_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true ...

  7. linux笔记:RPM软件包管理-rpm命令管理

    rpm包命名原则: rpm包的依赖性: 包名和包全名: rpm软件包安装.升级和卸载: rpm软件包查询: 从rpm包中提取指定文件:

  8. java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...

  9. bfs 胜利大逃亡

    http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一 ...

  10. 《javascript高级程序设计》第五章 reference types

    第5 章 引用类型5.1 Object 类型5.2 Array 类型 5.2.1 检测数组 5.2.2 转换方法 5.2.3 栈方法 5.2.4 队列方法 5.2.5 重排序方法 5.2.6 操作方法 ...