CF1988C Increasing Sequence with Fixed OR Solution
题意简述如下:
给定一个正整数 \(n\),请构造一个正整数序列使其满足以下条件并尽可能长:这个序列中每个数都大于等于 \(1\) 且小于等于\(n\);这个序列是单调递增的;这个序列中任意两个相邻的数按位或的结果都为 \(n\)。
通过手玩或者写个最长上升子序列可以发现,我们记这个数在二进制位上 \(1\) 的个数为 \(c\),当 \(c=1\) 时,序列最长的长度为 \(1\),否则为 \(c+1\)。
然后很显然的构造,当 \(c=1\) 时直接输出自身,否则这 \(c+1\) 个数中第 \(i\) 个数为将 \(n\) 从高位到低位第 \(i\) 个 \(1\) 改为 \(0\) 后的值,当然第 \(c+1\) 个数一定最大,就是 \(n\)。
最后为什么长度为 \(c+1\) 时是最大的,可以感性理解一下,显然上文中的构造方法是每一个数都是 \(n\) 的二进制下去掉一位,如果去掉的位数大于一位,又要保证其单调递增,那么长度就会小于 \(c+1\)。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL t[100], tot = 0;
int main() {
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
LL T, n; cin >> T;
while (T --) {
cin >> n; tot = 0;
for (LL i = 0; i <= 62; i ++) {
if ((n >> i) & 1) t[++ tot] = i;
}
if (tot == 1) {
cout << "1\n" << n << "\n";
continue;
} else cout << tot + 1 << "\n";
for (LL i = tot; i >= 1; i --) {
cout << (n ^ (1LL << t[i])) << " ";
}
cout << n << "\n";
}
return 0;
}
CF1988C Increasing Sequence with Fixed OR Solution的更多相关文章
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分
E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...
- Increasing Sequence CodeForces - 11A
Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...
- cf 11A Increasing Sequence(水,)
题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < ...
- Longest Increasing Sequence
public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...
- 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)
分析: 完整 代码: // 最长不下降子序列 #include <stdio.h> #include <algorithm> using namespace std; ; in ...
- Interview-Increasing Sequence with Length 3.
Given an array, determine whether there are three elements A[i],A[j],A[k], such that A[i]<A[j]< ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
- [Swift]LeetCode491. 递增子序列 | Increasing Subsequences
Given an integer array, your task is to find all the different possible increasing subsequences of t ...
随机推荐
- oidc-client.js踩坑吐槽贴
前言 前面选用了IdentityServer4做为认证授权的基础框架,感兴趣的可以看上篇<微服务下认证授权框架的探讨>,已经初步完成了authorization-code与implicit ...
- 利用QEMU模拟大端序机器
简介 当前我们安装虚拟机,一般小端机器比较多,有时候想模拟大端机器测试程序,这时就有模拟大端机器的需求. 参考:利用 QEMU USER 模式运行 mips 程序 - sinpo828 - 博客园 ( ...
- 在webpack中运行vue
网址:https://vue-loader.vuejs.org/zh/ Vue Loader 是一个 webpack 的 loader,它允许你以一种名为单文件组件的格式撰写 Vue 组件 安装loa ...
- 关于朋友圈出现的小米新店广告骗局(非法获取个人消息)木马通过广东政务服务网(tyrz.gd.gov.cn)的url漏洞显示
前两天在朋友圈突然看到有发 小米新店开业 送千台扫地机器人的 广告,出于天上不会掉馅饼到我身上的原则 我选择忽略了,但是没多久 看到他又晒了个物流订单,于是还是点开看了一下,发现微信打开的网站还蛮正规 ...
- The sultion of P4959
problem & blog 首先我们看到 \(x,y\) 有可能为负数,所以我们先把它旋转到第一象限. 然后我们发现如果 \(x_a \ge x_b\) 且 \(y_a \ge y_b\) ...
- spring jpa restful请求示例
创建项目 导入jar包mysql 数据库和连接池jar <dependency> <groupId>org.springframework.boot</groupId&g ...
- 解析Html Canvas的卓越性能与高效渲染策略
一.什么是Canvas 想必学习前端的同学们对Canvas 都不陌生,它是 HTML5 新增的"画布"元素,可以使用JavaScript来绘制图形. Canvas元素是在HTML5 ...
- FFmpeg GL-transition转场的简单使用体验
写在前面 最近在处理视频,遇到两个视频之间的转场用原生的 xfade写起来很痛苦,实现成本高,难度大:我这里主要用的FFmpeg,就想找一个插件专门干转场这个事:搜索了一翻后找到 GL-transit ...
- 蚁群算法及 TSP 问题上的应用
群智能(Swarm intelligence) 自然界动物群,称之为群. 群的特征: 相互作用的相邻个体的集合 个体的行为简单,既有竞争又有协作 智能化的集体行为(1+1>2): 个体间不仅能够 ...
- yolov1-yolov5 网络结构&正负样本筛选&损失计算
学习yolo系列,最重要的,最核心的就是网络模型.正负样本匹配.损失函数等三个方面.本篇汇总了yolov1-yolov5等5个版本的相关知识点,主要看点是在yolo框架搭建.初学者可以通过相关篇章搭建 ...