题意简述如下:

给定一个正整数 \(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的更多相关文章

  1. 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 ...

  2. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  3. Increasing Sequence CodeForces - 11A

    Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...

  4. cf 11A Increasing Sequence(水,)

    题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i <  ...

  5. Longest Increasing Sequence

    public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...

  6. 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)

    分析: 完整 代码: // 最长不下降子序列 #include <stdio.h> #include <algorithm> using namespace std; ; in ...

  7. 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]< ...

  8. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  10. [Swift]LeetCode491. 递增子序列 | Increasing Subsequences

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

随机推荐

  1. CSS——渐变色

    <!DOCTYPE html> <html> <head> <style> div { width: 210px; height: 50px; floa ...

  2. go 有向简单图 十字链表

    package main import "fmt" type CrossEdgeNode struct { tailVex int // 尾顶点 headVex int // 头顶 ...

  3. .NET 基础知识 单文件部署和可执行文件 剪裁独立部署和可执行文件

    单文件部署和可执行文件 https://docs.microsoft.com/zh-cn/dotnet/core/deploying/single-file     剪裁独立部署和可执行文件 http ...

  4. 理解太阳辐射 DNI DHI GHI

    理解太阳辐射 DNI DHI GHI   DNI: Direct Normal Irradiance 阳光从太阳盘面直接照射到与光路正交的表面,称作直接辐射简写为 DNI. DHI: Diffuse ...

  5. itestwork(爱测试)开源一站式接口测试&敏捷测试工作站 9.0.0 GA 发布,重大升级

    (一)itest 简介 itest work (爱测试)  一站式工作站让测试变得简单.敏捷.itest work 包含极简的任务管理,测试管理,缺陷管理,测试环境管理,接口测试,接口Mock 6合1 ...

  6. uniapp 跳转指定的tab导航页面

    考察的知识点:1.uniapp的页面跳转方法:uni.navigateTo2.页面跳转传值的方法:'?名称=值' 的方式3.参数的接收 onload(option){} 第一个页面的写法: toCom ...

  7. redis缓存详解(从入门到精通)

    引言 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(l ...

  8. ElasticSearch服务Java内存异常分析和排查解决

    ElasticSearch服务Java内存异常分析和排查解决 1.ElasticSearch业务微服务日志排查java.lang.IllegalStateException: Request cann ...

  9. maven和gradle环境变量配置及idea相关的设置

    1.maven 环境变量添加之后,重新打开cmd窗口,验证是否配置成功. idea   File >> Settings idea配置之后的验证: 2.gradle 环境变量添加之后,重新 ...

  10. 用 Python 绘制现金流量图

    目录 用 Python 绘制现金流量图 Python 实现 实现原理 具体代码 使用示例 1:根据现金流量表绘制现金流量图 使用示例 2:绘制等额.等差.等比序列现金流量图 等额序列现金流量图 等差序 ...