从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int sum[],num[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
memset(sum,,sizeof(sum));
memset(num,,sizeof(num));
long long n;
int m;
cin>>n>>m;
long long x=n;
int cnt2=;
while(n){
if(n&)
++sum[cnt2];//将n转化为二进制
n/=;
++cnt2;
}
long long res=;
for(int i=;i<=m;++i){
cin>>a[i];
res+=a[i];
int cnt=-;
while(a[i]){
a[i]/=;
++cnt;
}
++num[cnt];//将a[i]转化为二进制
}
if(res<x){
cout<<"-1\n";
continue;
}
long long ans=;
for(int i=;i<;++i){//从低位向高位枚举
if(sum[i]){
if(num[i])
--num[i];//如果有对应a[i]直接取
else{
for(int j=i+;j<;++j){//没有的话就找到最近的一个a[i]
if(num[j]){
--num[j];//把它一步步拆掉,直到二进制下i位有一个可取
for(int k=i;k<j;++k){
++num[k];
}
ans+=j-i;//一共拆了j-i次
break;
}
}
}
}
num[i+]+=num[i]/;//多余的a[i]向上合并成为更大的a[i+1]
}
cout<<ans<<"\n";
}
return ;
}

最新文章

  1. redis 基础
  2. [转]easyui tree 模仿ztree 使用扁平化加载json
  3. REACT 学习
  4. EBS R12.2应用层关闭脚本的执行过程
  5. 高速公路(Highway,ACM/ICPC SEERC 2005,UVa1615)
  6. Windows-008-VirtualBox 安装 Win7 前沿配置详解
  7. node.js学习(1)
  8. 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
  9. linux命令之ps命令
  10. -join 和 -split 用法
  11. matlab中的三维坐标系与旋转
  12. 函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义
  13. win8系统特别慢的基本判断方法
  14. Lintcode469-Same Tree-Easy
  15. C# 一般处理程序下载文件
  16. node.js初识11
  17. Codeforces 839C Journey - 树形动态规划 - 数学期望
  18. ubuntu图形化配置
  19. 【转帖】如何在redhat单机服务器上运行postgresql的多个实例(howto run multiple postgresql instance on one redhat server)
  20. UDP丢包问题

热门文章

  1. arcgis10.2下载安装教程
  2. Python GUI编程(TKinter)(简易计算器)
  3. BDA3 Chapter 1 Probability and inference
  4. Python论做游戏外挂,Python输过谁?
  5. linux--后端项目部署
  6. Pytest学习10-pytest与unittest的区别
  7. Spring Boot的Web配置
  8. SpringMVC组件解析
  9. python dataframe筛选列表的值转为list【常用】
  10. HCTF2018-admin[条件竞争]