[NOIP2017模拟]鱼群分裂

本文通过一道编程题探讨了使用暴力深度优先搜索(DFS)解决特定类型问题的有效性和局限性。作者分享了一种针对给定整数N和K进行拆分的算法实现,并通过实例数据展示了如何在限制条件下寻找解决方案的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017.10.13 T1 1977

样例数据
输入

6 2

输出

3

分析:我之所以要写这道题的博客,不是因为它难而是我要吐槽!看看这数据,光是想到K=0,N=229时dfs绝对会被卡得飞起,但是,出题人wuvin表示对于K>0可以证明复杂度不大于O(KlogNx)(我也没听懂……反正好像很对,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;
}

本题结。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值