当前位置: 首页 > news >正文

Pseudo Pseudo Random Numbers

题目描述

It turns out that if numbers were truly random, then each possible bit string (string of 0’s and 1’s) of length n would be equally likely.  For example, 111111 would be just as likely as 010110 to occur.  Unfortunately, most people believe that any time they see the same bit over and over again, that the process can't be truly random. 

You are in charge of generating random bit strings of length n for use in a video game.  However, the producer of the game has asked you to remove all possibilities where there are more than k 0’s or 1’s in a row.  For example, if n = 4 and k = 2, then the 10 valid bit strings would be 0010, 0011, 0100, 0101, 0110, 1001, 1010, 1011, 1100, and 1101 (the other 6 strings of 4 bits either have more than two 0’s in a row or more than two 1’s in a row, so they are not valid). 

Given the values of n and k, determine the number of n bit strings that do not contain any runs of 0’s or 1’s of length greater than k. 

输入

There is only one input line; it contains two integers: n (2 ≤  n ≤ 20), indicating the length of the bit string for the problem, and k (1 ≤  k ≤  n), indicating the maximal length of a run of 0’s or 1’s for the bit strings to be created. 

输出

Print the number of valid bit strings of length n that do not contain any runs of the same bit of length greater than k. 

样例输入 

【样例1】
4 2
【样例2】
5 1
【样例3】
20 20

样例输出 

【样例1】
10
【样例2】
2
【样例3】
1048576

思路:数据都很小,直接暴力,生成所有的情况再进行检查,重点学会怎么生成所有的情况(递归)

AC代码

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<string>a;
int n,k;
//生成所有可能的串
void fun(int n,string s)
{if (s.size()==n){a.push_back(s);return ;}fun(n,s+'0');fun(n,s+'1');	
}
bool check(string s)
{int cnt=1;for (int i=1;i<s.size();i++){if (s[i]==s[i-1]){cnt++;if (cnt>k)return false;}else{cnt=1;	}}return true;
}
int main()
{cin>>n>>k;fun(n,"");int ans=0;for (auto i:a){//cout<<i<<endl;//检查每个字符串是否满足if (check(i))ans++;}cout<<ans;
}

http://www.dtcms.com/a/315552.html

相关文章:

  • Docker使用的常见问题
  • 《BFC的深层逻辑与全域应用》
  • 目标检测、分割的数据增强策略
  • 前端安全攻防
  • CVE-2017-8291源码分析与漏洞复现(PIL远程命令执行漏洞)
  • Kafka-Eagle 安装
  • LeetCode——2411. 按位或最大的最小子数组长度
  • 工业级 CAN 与以太网桥梁:串口服务器CAN通讯转换器深度解析(上)
  • 【Git】git提交代码报错Git: husky > pre-commit
  • 【java】大数据insert的几种技术方案和优缺点
  • 机器学习——集成学习(Ensemble Learning)详解:原理、方法与实战应用
  • 机遇识别与商业变革:基于开源AI大模型、AI智能名片与S2B2C商城小程序的协同创新研究
  • 【Day 16】Linux-性能查看
  • SpringBoot3.x入门到精通系列:4.3 性能优化技巧
  • 飞算JavaAI需求转SpringBoot项目:从零到一的沉浸式开发之旅
  • Angular进阶之十三:Angular全新控制流:革命性的模板语法升级
  • Solidity智能合约基础
  • Python 函数详解
  • 精华贴分享|指数,衍生品,与交易时间之间的逻辑关系
  • Apache OFBiz Scrum 组件命令注入漏洞
  • MySQL 查询性能优化与索引失效问题全解析
  • 视频水印技术中的变换域嵌入方法对比分析
  • K8s Master状态NotReady
  • Linux内核参数调优:为K8s节点优化网络性能
  • Datart:开源数据可视化的新星,赋能企业数据分析
  • K8S的NetworkPolicy使用教程
  • ubuntu24中部署k8s 1.30.x-底层用docker
  • 本机部署K8S集群
  • 基于k8s环境下的pulsar常用命令(下)
  • 查看部署在K8S服务的资源使用情况