首页 > 其他分享 >B. Make Array Good【二进制构造】

B. Make Array Good【二进制构造】

时间:2023-03-20 17:48:36浏览次数:49  
标签:小数 Good int Make Array include 公因数

B. Make Array Good

https://codeforces.com/problemset/problem/1762/B
image
image

思路

  1. 将不是\(2^n(n>0)\)的数构造成最小的一个大于\(a[i]\)的\(2^n\),
    证明:

\[a[i]_{new} = 2^n = a[i] + x(0 \le x \le a[i]) \]

\[a[i] > 2^{n-1} \]

\[x = 2^{n} - a[i] < 2^{n} - 2^{n-1} = 2^{n-1} < a[i] \]

代码

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<cctype>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 1e5+10;
const int M = 2e5+10;
int a[N];

void solve(){
	int t;
	cin >> t;
	while(t -- ){
		int n;
		cin >> n;
		vector<pair<int,LL>> v;
		for(int i = 1; i <= n; i ++ ){
			cin >> a[i];
			if(__builtin_popcount(a[i]) != 1 && a[i] != 1){
				v.push_back({i,(1ll << (32 - __builtin_clz(a[i]))) - a[i]});
			}
		}
		cout << v.size() << nl;
		for(auto u:v){
			cout << u.X << " " << u.Y << nl;
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}

二进制运算一些技巧

  1. __builtin_popcount(a[i]) != 1 ->不是2的幂
  2. 1ll << (32 - __builtin_clz(a[i])最小的一个大于a[i]的2的幂

公因数(数论角度)

  1. 原题条件可由大数被小数整除转化为小数是大数和小数的最大公因数
  2. 所有\(2^n(n > 0)\)的数最大公因数都为小数

标签:小数,Good,int,Make,Array,include,公因数
From: https://www.cnblogs.com/J-12045/p/17237072.html

相关文章

  • Error in invoking target 'mkldflags ntcontab.o nnfgt.o' of makefile
     安装数据库报错:./runInstaller-silent-force-ignorePrereq-showProgress-responseFile/u01/software/database/response/my_db_install.rspErrorininvokingt......
  • Qt5.12实战之QByteArray数据转换处理
    效果:视频:​​https://www.kuaishou.com/short-video/3xje8ib9gj68u52?authorId=3x8b3wqc6r8q766&streamSource=profile&area=profilexxnull​​示例源码:#include<QCo......
  • 560.Subarray Sum Equals K
    Givenanarrayofintegersandaninteger k,youneedtofindthetotalnumberofcontinuoussubarrayswhosesumequalsto k.Example1:Input:nums=[1,1,1]......
  • Qt5.12实战之QByteArray与字符指针及字符串转换
    示例源码:#include<QCoreApplication>#include<QDebug>#include<QTextStream>staticQTextStreamcout(stdout,QIODevice::WriteOnly);#include<iostream>#include......
  • Qt5.12实战之字节数组QByteArray使用
    示例源码:#include<QCoreApplication>#include<QDebug>#include<QTextStream>staticQTextStreamcout(stdout,QIODevice::WriteOnly);#include<iostream>#inclu......
  • Goods
    selected=sort_links.loc[sort_links['Types']=='果蔬']#挑选商品类别为“果蔬”并排序child_nums=selected['id'].sum()#对所有的“果蔬”求和selected['c......
  • std::array
    array::arrayarray<typename,size>name;(typenamename[size])在越界的时候会加以警告。(再也不用调这玩意啦!)取某一个元素和普通数组一致。fill(t);可以直接这样写......
  • Array数组
    数组: 数组是相同类型数据的有序集合 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成 每一个数据称作一个数组元素,每个数组元素都可以通过......
  • cpp generate template number and fill array then order by quick sort
    //model/util.htemplate<typenameT>Tgen_random_num(Tmin,Tmax);voidgen_uint64(constint&len);voidgen_uint32(constint&len);templat......
  • java进阶 -System类 -currentTimeMillis返回时间毫秒值 -arraycopy数组拷贝40
      currentTimeMillis:packagecom.cyjt97.SY;publicclassay{publicstaticvoidmain(String[]args){longstart=System.currentTimeMillis(......