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 ...
随机推荐
- VisioForge.DotNet.Core.UI.WPF WPF摄像头 UVC 显示 支持 .net core
Sample applications available at https://github.com/visioforge/.Net-SDK-s-samples . Please add Visio ...
- NOIP模拟56
前言 话说, T2 和 T3 的题面好像放反了. T1 爆零 解题思路 是个原题.. 当时 WindZR 25分钟就码完了,然后我就慌死,写完就开始调,诶,我当时场上不是切了吗,怎么现在打不对了.. ...
- itest work 开源接口测试&敏捷测试管理平台 9.5.0 GA_u1,优化及修复关键 BUG
(一)itest work 简介 itest work (爱测试) 一站式工作站让测试变得简单.敏捷,"好用.好看,好敏捷" ,是itest wrok 追求的目标.itest w ...
- ssh 端口转发实验
为什么会使用端口转发 端口转发的优点: 安全性:通过隐藏实际服务(在这种情况下是监听在22端口的SSH服务)的真实端口号,增加了一层安全性.攻击者可能不知道真正的服务端口号,因此更难进行有针对性的攻击 ...
- map数据类型
MAP数据类型是一个类似于对象的数据类型 大型项目中会经常使用 通过 构造函数来定义MAP数据类型 con ...
- TQX 的 DP AAgain!
闲话: 这确实抽象,将所有人给干离线了-- 不如叫做 TQX 的离线 DP QwQ DP 基本思路就是找一个比较好的能够描绘问题的状态,想怎么转移,再进行优化. --TQX 背包 DP loj 608 ...
- django通过celery定时任务
settings.py # Broker配置,使用Redis作为消息中间件 BROKER_URL = 'redis://127.0.0.1:6379/0' # BACKEND配置,这里使用redi ...
- 18.9k star!一个高性能的嵌入式分析型数据库,主要用于数据分析和数据处理任务。
大家好,今天给大家分享的是一个开源的面向列的关系数据库管理系统(RDBMS). DuckDB是一个嵌入式的分析型数据库,它提供了高性能的数据分析和数据处理能力.DuckDB的设计目标是为数据科学家.分 ...
- ObjectMapper Json字符串的转换处理
package com.example.demo; import com.example.pojo.User; import com.fasterxml.jackson.annotation.Json ...
- 效率提升利器:一个在线的.NET源码查询网站
前言 你是否有这样的苦恼,有时候需要查询.NET中的某个类型.方法.属性或程序集的源代码,但又不想从GitHub中下载源代码.今天大姚分享一个在线且实用的.NET源码查询网站. 在线查询地址 http ...