1. 笔记

比较容易的动态规划题。往左很好考虑,往右用dpi表示前i只都被k吃掉后,k继续往右仍然不死的情况数。状态转移方程为dp[I]=dp[I+1]+...+dp[j],分别对应第I+1位向左,...,第j位向左(I和j之间的都向右)。其中j为满足条件的最大的蚂蚁(如果I+1到j都向右,j向左,那么k死)。貌似这个dp和网上一些题解不太一样,不过没细看。这些都不要紧,关键的是写完代码各自玄学TLE,花了足足五个小时才改到A,最后发现罪魁祸首是sqrt。。。吐血。以前从来没注意过这个,今天才发现凡是小数运算都奇慢无比,真是血的教训啊。

2. 代码

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define ms(arr,a) memset(arr,a,sizeof arr)
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
typedef long long ll;
const int maxn=1e6+5;
const ll mod=1e9+7;
int pos[maxn];
int dp[maxn];
int n,k;
int quick_pow(int a,int n)
{
register int ret=1;
while(n)
{
if(n&1)ret=1LL*ret*a%mod;
a=1LL*a*a%mod;
n>>=1;
}
return ret;
}
inline ll sum(int x)
{
return 1LL*x*(x+1);
}
int bs(int l,int r,int i)
{
int m;bool ll=(sum(l)<=2*sum(i)),rr=(sum(r)<=2*sum(i));
if(ll&&rr)return r;
if(!ll&&!rr)return -1;
if(!ll){r=r^l;l=r^l;r=r^l;}
while(r-l>1||l-r>1)
{
m=(l+r)/2;
if(sum(m)<=2*sum(i))l=m;
else r=m;
}
return l;
}
int main()
{
//freopen("Input.txt","r",stdin);
//freopen("1.txt","w",stdout);
for(int i=1;i<maxn;++i)pos[i]=bs(i+1,maxn,i);
int T;scanf("%d",&T);
rep(Case,1,T)
{
scanf("%d%d",&n,&k);
if(k==1){printf("Case #%d: 0\n",Case);continue;}
dp[n]=1;dp[n+1]=0;
for(int i=n-1;i>=k;--i)
{
int j=pos[i];
j=min(n,j);
dp[i]=((2*dp[i+1]-dp[j+1])%mod+mod)%mod;
}
int l=1,m,r=k-1;
while(l<r)
{
m=(l+r+1)/2;
if(2*sum(m)<sum(k))l=m;
else r=m-1;
}
//l=ceil(sqrt(k*(k+1)/2+0.25)-0.5)-1;
printf("Case #%d: %lld\n",Case,1LL*quick_pow(2,l+1)*(dp[k]-dp[k+1]+mod)%mod);//
}
//freopen("CON","w",stdout);
//system("start Output.txt");
}

最新文章

  1. 如何绕过chrome的弹窗拦截机制
  2. html span标签 不换行(有时span带中文时候是可以自动换行的)
  3. PL/SQL设置编码方式
  4. 老男孩linux高级架构 百度云盘下载
  5. [Java]Java简介
  6. Cadence Allegro小技巧-从外部文本文件添加文本
  7. stm32 堆和栈(stm32 Heap &amp; Stack)【worldsing笔记】
  8. 暑假集训(3)第三弹 -----Til the Cows Come Home(Poj2387)
  9. VC++在对话框中加入属性页
  10. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
  11. [转]如何使用PHP实现javascript的escape和unescape函数
  12. Linux 查看CPU温度
  13. 记Javascript一道题的理解
  14. windows下安装git和vundle
  15. y7000笔记本 darknet-yolo安装与测试(Ubuntu18.04+Cuda9.0+Cudnn7.1)
  16. go中rune和byte的用处
  17. SystemParametersInfo调置壁纸、屏幕保护程序
  18. mysql innodb引擎 一次线上死锁分析排查步骤
  19. Androidstudio中jar包重复或jar包里的类重复问题
  20. 系统间通信——RPC架构设计

热门文章

  1. Zookeeper是如何实现分布式锁的
  2. jq日历一周为单位轮播
  3. Centos下载新版内核
  4. json的fromjson的方法使用。可以在volley中进行使用
  5. 100 Path Sum
  6. go1.13errors的用法
  7. termux上安装lxml失败
  8. Apache SkyWalking
  9. 批量重命名脚本(Python)
  10. 今天我们来谈谈jquery,