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 ...
随机推荐
- mac goland go env 环境变量 和 mac上终端go env 不一样
编辑 vim ~/.zshrc. 在这里设置环境变量.goland 里面的才会生效.设置bash_profile 没用.
- Qt-FFmpeg开发-视频播放(5)
音视频/FFmpeg #Qt Qt-FFmpeg开发-视频播放[软/硬解码 + OpenGL显示YUV/NV12] 目录 音视频/FFmpeg #Qt Qt-FFmpeg开发-视频播放[软/硬解码 + ...
- 一文带你理解透MyBatis源码
本文分享自华为云社区<一文彻底吃透MyBatis源码!!>,作者:冰 河. 写在前面 随着互联网的发展,越来越多的公司摒弃了Hibernate,而选择拥抱了MyBatis.而且,很多大厂在 ...
- c# 拖拽列表顺序 | 拖拽合并分组 | 移除分组功能
动图演示: 背景: 一开始做功能的时候没有增加排序的索引(sort-index),后来要求做拖拽排序功能:所以写了这个不需要初始排序就可以完成的拖拽功能:如果是table表格排序逻辑和这个相似,这里拿 ...
- react props进阶 children属性
children属性,表示组件标签的子节点,当组件标签有子节点时,props就会有该属性,与与普通的props一样,其值可以使任意类型. # 父组件 class App extends React.C ...
- 深入剖析C++多态的实现与原理-详解
目录 多态基础 虚函数 虚函数的继承 虚类/虚基类 重写/覆盖 条件: 概念: 多态的条件 其他的多态行为 多态中子类可以不写virtual 协变 代码举例 继承遗留问题解决 析构函数 具体解决方式: ...
- INFINI Labs 产品更新 | 修复 Easysearch 跨集群复制索引同步问题,Gateway 内存异常增长等问题
INFINI Labs 产品又更新啦~,本次更新主要对 Easysearch.Gateway.Console.Agent 等产品功能进行优化和相关 Bug 修复,解决了内存异常增长等问题,以下是详细说 ...
- Truncate的使用详解
删除表中数据的方法有 delete 和 truncate, 其中TRUNCATE TABLE用于删除表中的所有行,而不记录单个行删除操作:TRUNCATE TABLE 与没有 WHERE 子句的 DE ...
- 关于编译告警 C4819 的完整解决方案 - The file contains a character that cannot be represented in the current code page (number). Save the file in Unicode format to prevent data loss.
引言 今天迁移开发环境的时候遇到一个问题,同样的操作系统和 Visual Studio 版本,原始开发环境一切正常,但是迁移后 VS 出现了 C4819 告警,上网查了中文的一些博客,大部分涵盖几种解 ...
- iOS:长图切割并转为动画gif——精灵表单sprite Sheet的转化
iOS:长图切割并转为动画gif--精灵表单sprite Sheet的转化 通常的,iOS显示gif可以将文件转为NSData后再对其进行解析,通过CADisplayLink逐帧进行提取.播放,判断N ...