codeforces 336C Vasily the Bear and Sequence
来源:互联网 发布:钱德勒宾 知乎 编辑:程序博客网 时间:2024/05/14 20:51
点击打开codeforce 336C
思路; 位运算
分析:
1 题目要求找到k个数,使得这k个数的&的结果能够被2^v整除,并且v的值应该要尽量的大
2 一个数能够被2^v整数,说明这个数的二进制的最右边的1后面刚好是v个0,比如20的二进制为10100,那么刚好可以被2^2整除,那么就有最右边的1的后面0的个数刚好为2个
3 那么我们可以通过从大到小枚举v,然后我们去n个数里面找满足“这个数的二进制的最右边的1后面刚好是v个0”,然后进行求&运算,只要我们找到一个满足的就是最后的ans
代码:
#include<set>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 100010;int n;int num[MAXN];void solve(){ set<int>s; for(int v = 30 ; v >= 0 ; v--){ int val = 1<<v; int sum; s.clear(); for(int i = 0 ; i < n ; i++){ if(num[i]&val){ if(s.empty()) sum = num[i]; else sum &= num[i]; s.insert(num[i]); } } if(sum%val == 0){ printf("%d\n" , s.size()); set<int>::iterator it = s.begin(); printf("%d" , *it++); for(; it != s.end() ; it++) printf(" %d" , *it); puts(""); return; } }}int main(){ while(scanf("%d" , &n) != EOF){ for(int i = 0 ; i < n ; i++) scanf("%d" , &num[i]); solve(); } return 0;}
- codeforces 336C Vasily the Bear and Sequence
- Codeforces 336C Vasily the Bear and Sequence 暴力
- Codeforces 336C Vasily the Bear and Sequence (暴力)
- Codeforces Round #195 (Div. 2) C. Vasily the Bear and Sequence
- Codeforces Round #195 (Div. 2) C--Vasily the Bear and Sequence(贪心)
- CF 336C(Vasily the Bear and Sequence-贪心-不满足单调性)
- Codeforces 336A【 Vasily the Bear and Triangle 】
- Codeforces-336A-Vasily the Bear and Triangle
- CodeForces #195 Div.2-A. Vasily the Bear and Triangle
- [Codeforces Round #195 DIV2B (CF336B)] Vasily the Bear and Fly
- Codeforces Round #195 (Div. 2) / 336A Vasily the Bear and Triangle(模拟&数学)
- Codeforces 336D. Vasily the Bear and Beautiful Strings 解题报告
- Codeforces 336D Vasily the Bear and Beautiful Strings 组合数学
- CF 336A(Vasily the Bear and Triangle-推公式)
- A. Vasily the Bear and Triangle
- Codeforces Round #195 (Div. 2) A Vasily the Bear and Triangle
- Codeforces Round #195 (Div. 2) B. Vasily the Bear and Fly - 找规律
- [Codeforces Round #195 DIV2E (CF336E)] Vasily the Bear and Painting Square
- Java中线程安全的单例模式
- Hdu 4422 The Little Girl who Picks Mushrooms
- [项目心得]自己的第一个项目2
- hdu1402 A * B Problem Plus 高精度乘法 快速傅里叶变换(FFT)
- 语录
- codeforces 336C Vasily the Bear and Sequence
- statistics_level参数说明
- MFC中CString转int/double/TCHAR*类型
- SSDT
- POJ 1270 Following Orders (拓扑 + DFS回溯)
- 八个移动产品设计必备网站
- char*,string,CString,int,char[]之间的转换
- ie overflow无效解决办法
- Java权限限制符的一些记录