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 ...
随机推荐
- CSS——圆角
例子1: <!DOCTYPE html> <html lang="en"> <head> <style> div { width: ...
- C#复杂类型转为QueryString
使用 visual studio 创建 webapi 项目,并添加 DefaultController.cs,代码如下 public class DefaultController : ApiCont ...
- Flutter(九)Json序列化与反序列化(转Model)
在日常开发中JSON的序列化与反序列化是一个常见的操作:而Dart语言不支持反射,运行时反射会影响Dart的tree shaking(摇树优化),tree shaking可以"抖掉" ...
- 鸿蒙HarmonyOS实战-Web组件(页面跳转和浏览记录)
前言 页面跳转是指在浏览器中从当前页面跳转到另一个页面的操作.可以通过点击链接.输入网址.提交表单等方式实现页面跳转. 浏览记录是指记录用户在浏览器中浏览过的页面的历史记录.当用户跳转到一个新页面时, ...
- 关于前三次pta的总结
前言 这三次pta难度在不断上升的同时,要求我们线上慕课+自主学习来了解更多的java中的各种方法,如:正则表达式 List Map等.与此同时要求我们展开尝试并熟练类的构造,类的引用,链表的基本运用 ...
- php监控
1.开启php的监控数据监控功能 # 使用部署了php-fpm的机器即可 # yum install php-fpm -y # 1.修改参数 [root@web-7 /etc/php-fpm.d]#g ...
- MESI--CPU缓存一致性协议
概念 MESI(Modified Exclusive Shared Or Invalid)(也称为伊利诺斯协议,是因为该协议由伊利诺斯州立大学提出)是一种广泛使用的支持写回策略的缓存一致性协议. ME ...
- Apollo config配置中心 配置列表和map DEMO
Apollo config配置中心 配置列表和map DEMO#支持可扩展 Apollo配置 apollo中配置如下: defaultId = 100,200 chooseId = {"30 ...
- 幻想领域图床系统V1.2正式版发布
Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 幻想领域图床系统V1.2正式版发布 日期:2018-4-1 ...
- golang如何使用指针灵活操作内存?unsafe包原理解析
Hi 你好,我是k哥.一个大厂工作6年,还在继续搬砖的后端程序员. 我们都知道,C/C++提供了强大的万能指针void*,任何类型的指针都可以和万能指针相互转换.并且指针还可以进行加减等算数操作.那么 ...