当前位置:网站首页>C. Travelling Salesman and Special Numbers (二进制 + 组合数)
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
2022-07-30 04:09:00 【小酒窝.】
C. Travelling Salesman and Special Numbers
题意
给定一个数 n,问 1 - n 中一共有多少数能够经过 m 次下面的转换变为 1?
- 对于一个数 x,如果其对应二进制一共有 k 位为 1,那么 x 变为 k。
( 1 ≤ n < 2 1000 , 0 ≤ m ≤ 1000 ) (1 ≤ n < 2^{1000},\ 0 ≤ m ≤ 1000) (1 ≤ n < 21000, 0 ≤ m ≤ 1000)
n 给出无前导零的二进制。 n\ 给出无前导零的二进制。 n 给出无前导零的二进制。
思路
虽然 n 很大,但是再大的数执行一次转换之后都小于 1000 了。
假设执行过一次操作变为 x,x 经过 k 次操作变为了1,那么 n 就要执行 k+1 次操作变为1。
所以可以先暴力求出 1-1000 中所有数分别需要多少次转换变为1,假设一个数 x 经过 k 次操作变为1,那么如果一个数经过 k+1 次操作变为1,其二进制中1的个数就为 x。
对于每个操作次数 k,提前存储其对应的原数二进制表达中 1 的个数。
例如:如果操作次数为 2,那么原数二进制有就应该有 2 或 4 或 8 或 16 … 个 1。
现在给定了 操作次数为 m,我们需要根据给出的 n 的二进制表达找到 1-n 中一共有多少数满足其二进制有 v[0], v[1], v[2]… 个 1。
对于 n 的二进制每一位 1 来说,如果这一位 1 变为 0 了,那么修改过的数无论后面所有位是 0 是 1,都比原来的 n 小。假设前面位置一共有 cnt 个1,后面还有 left 个位置,那么就可以从后面的所有位置中选取 v[i] - cnt 个 和 cnt 组成一共 v[i] 个1。
遍历所有 1 的位置,对于每个位置都遍历需要的 1 的个数,求组合数累加。
int cnt = 0;
for(int i=1;i<=n;i++)
{
if(a[i] != '1') continue;
for(int x : vv)
{
int left = n - i;
if(cnt > x) continue;
if(left < x-cnt) continue;
ans = (ans + C[left][x-cnt]) % mod;
}
cnt ++;
}
需要注意的细节:
- 上面是判断的 1~n-1 的数,n 本身的 1 的个数还要判断是否是需要满足的 1 的个数,如果是,ans ++;
- 当操作数为 1 时,需要满足二进制中有 1 个 1,但是如果这个数是 1 的话,其操作数应该为 0,需要删掉原数为 1 的情况;
- 当操作数为 0 时,原数为 1,n ≥1 一定满足,答案一定为 1。
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define int long long
const int N = 200010, mod = 1e9+7;
int T, n, m;
char a[N];
vector<int> v[20];
int C[1010][1010];
void init()
{
for(int i=1;i<1000;i++)
{
int x = i;
int cnt = 0;
while(x != 1)
{
cnt ++;
bitset<12> f(x);
x = f.count();
}
v[cnt+1].push_back(i);
}
C[0][0] = 1;
for(int i=1;i<=1000;i++)
{
C[i][0] = 1;
for(int j=1;j<=i;j++)
{
C[i][j] = C[i-1][j-1] + C[i-1][j];
C[i][j] %= mod;
}
}
}
signed main(){
Ios;
init();
cin >> a+1;
n = strlen(a+1);
cin >> m;
if(m > 5){
cout << 0; return 0;
}
if(m == 1 && n == 1){
cout << 0; return 0;
}
if(m == 0){
cout << 1; return 0;
}
vector<int> vv = v[m];
int ans = 0;
if(m == 1) ans --;
int t = 0;
for(int i=1;i<=n;i++) if(a[i] == '1') t++;
for(int x : vv) if(x == t) ans ++;
int cnt = 0;
for(int i=1;i<=n;i++)
{
if(a[i] != '1') continue;
for(int x : vv)
{
int left = n - i;
if(cnt > x) continue;
if(left < x-cnt) continue;
ans = (ans + C[left][x-cnt]) % mod;
}
cnt ++;
}
cout << ans;
return 0;
}
边栏推荐
- 一起来学习flutter 的布局组件
- 小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
- 2022-07-29 Group 4 Self-cultivation class study notes (every day)
- CMake installation and testing
- 小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT
- Go书籍大全-从初级到高级以及Web开发
- After 5 years of Ali internship interview~
- Wechat second-hand transaction small program graduation design finished product (1) Development overview
- sqlmap use tutorial Daquan command Daquan (graphics)
- ospf 综合实验(重发布,特殊区域)
猜你喜欢
SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
PyG搭建R-GCN实现节点分类
Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
Has been empty, a straightforward, continue to copy the top off!
Roperties class configuration file & DOS to view the host network situation
How to solve the error "no such file or directory" when EasyCVR starts?
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat
数组和结构体
Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
随机推荐
Tcp programming
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
Boss Rush (二分答案 + 状压DP)
How to solve the error "no such file or directory" when EasyCVR starts?
vscode debugging and remote
新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
Pytorch框架学习记录2——TensorBoard的使用
数组和结构体
The difference between forward and redirect
Taobao/Tmall get the list of sold product orders API
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
RRU, BBU, AAU
高并发框架 Disruptor
[Driver] udev aliases each serial port of USB to 4 serial ports
ospf map
The difference between BGP room and ordinary room in Beijing
Eureka注册中心
New interface - API interface for "Taote" keyword search
golang中如何比较struct,slice,map是否相等以及几种对比方法的区别
Reverse Analysis Practice 2