2017.10.13 T1 1977
样例数据
输入
6 2
输出
3
分析:我之所以要写这道题的博客,不是因为它难而是我要吐槽!看看这数据,光是想到K=0,N=229时dfs绝对会被卡得飞起,但是,出题人wuvin表示对于K>0可以证明复杂度不大于O(K(logN)x)(我也没听懂……反正好像很对,x表示有很多个logN),而对于K=0,我们还是不黑心就没出数据orz
代码
花了两个小时思考,无奈之下写下的dfs暴力竟成为AC代码,天理呢?(虽然感觉很庆幸,但还是好难受,我的时间TAT)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
#include<set>
using namespace std;
int getint()
{
int sum=0,f=1;
char ch;
for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
if(ch=='-')
{
f=-1;
ch=getchar();
}
for(;isdigit(ch);ch=getchar())
sum=(sum<<3)+(sum<<1)+ch-48;
return sum*f;
}
int n,k,ans=1;
void dfs(int x)
{
int res1=x+k,res2=x-k;
if(res2>0&&res1%2==0)
{
ans++;
res1=res1/2;
dfs(res1);
res2=res2/2;
dfs(res2);
}
}
int main()
{
freopen("split.in","r",stdin);
freopen("split.out","w",stdout);
n=getint(),k=getint();
if((k%2==1&&n%2==0)||(k%2==0&&n%2==1))//为了骗分写的特判
cout<<1<<'\n';
else
{
dfs(n);
cout<<ans;
}
return 0;
}
本题结。