B.Petr and a Combination Lock
https://codeforces.com/contest/1097/problem/A
1 second
256 megabytes
standard input
standard output
Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of 360360 degrees and a pointer which initially points at zero:
Petr called his car dealer, who instructed him to rotate the lock's wheel exactly nn times. The ii-th rotation should be aiai degrees, either clockwise or counterclockwise, and after all nn rotations the pointer should again point at zero.
This confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all nn rotations the pointer will point at zero again.
The first line contains one integer nn (1≤n≤151≤n≤15) — the number of rotations.
Each of the following nn lines contains one integer aiai (1≤ai≤1801≤ai≤180) — the angle of the ii-th rotation in degrees.
If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word "YES". Otherwise, print "NO". Petr will probably buy a new car in this case.
You can print each letter in any case (upper or lower).
3
10
20
30
YES
3
10
10
10
NO
3
120
120
120
YES
In the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.
In the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.
In the third example, Petr can do all three rotations clockwise. In this case, the whole wheel will be rotated by 360360 degrees clockwise and the pointer will point at zero again.
1左移i位, 然后与c按位与。
&当两个操作数对应位都是1,结果才是1.
而1<<i 只有右数第i位是1, 其他都是0.
那么要结果非0, 除非c的第i位也是1.
所以 这个就是判断c的第i位是否为1, 如为1, 那么if成立。 否则if不成立。
PS:这里说的第i位都是从0计数的。
所以此处的意思从1开始遍历全部+1,-1的过程
递推
/*
Author: LargeDumpling
Email: LargeDumpling@qq.com
Edit History:
2019-01-04 File created.
*/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=;
int n,a[MAXN],limit;
int main()
{
bool flag=false;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
limit=<<n;
for(int S=;S<limit;S++)
{
int sum=;
for(int i=;i<n;i++)
if((S>>i)&) sum+=a[i];
else sum-=a[i];
if(sum%==)
flag=true;
}
if(flag) puts("YES");
else puts("NO");
fclose(stdin);
fclose(stdout);
return ;
}
递归 dfs
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int MOD=1e9+;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,ans;
int a[N];
char str;
void dfs(int c,int x){
if(c>=n){
if(x%==)
ans=;
return;
}
if(ans)
return;
dfs(c+,x+a[c+]);
dfs(c+,x-a[c+]);
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>a[i];
}
dfs(,);
if(ans)
cout << "YES" << endl;
else
cout << "NO" << endl; return ;
}
B.Petr and a Combination Lock的更多相关文章
- Petr and a Combination Lock
Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to r ...
- CF1097B Petr and a Combination Lock 题解
Content 有一个锁,它只有指针再次指到 \(0\) 刻度处才可以开锁(起始状态如图所示,一圈 \(360\) 度). 以下给出 \(n\) 个操作及每次转动度数,如果可以通过逆时针或顺时针再次转 ...
- Combination Lock
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a Micr ...
- hihocoder #1058 Combination Lock
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...
- 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock
题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...
- Codeforces Round #301 (Div. 2) A. Combination Lock 暴力
A. Combination Lock Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...
- Hiho----微软笔试题《Combination Lock》
Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You ...
- CF #301 A :Combination Lock(简单循环)
A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:
- hihocoder-第六十一周 Combination Lock
题目1 : Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview roo ...
随机推荐
- 【python】对于程序员来说,2018刑侦科推理试卷是问题么?
最近网上很火的2018刑侦科推理试卷,题目确实很考验人逻辑思维能力. 可是对于程序员来说,这根本不是问题.写个程序用穷举法计算一遍即可,太简单. import itertools class Solu ...
- 浙大PAT CCCC L3-013 非常弹的球 ( 高中物理题 )
题目链接 题意 : 刚上高一的森森为了学好物理,买了一个“非常弹”的球.虽然说是非常弹的球,其实也就是一般的弹力球而已.森森玩了一会儿弹力球后突然想到,假如他在地上用力弹球,球最远能弹到多远去呢?他不 ...
- 配置服务器(anaconda + jupyter + R)
总结一下整体的流程: 1. 服务器安装anaconda 首先清华镜像站下载anaconda3. 2. 将下载好的文件用scp命令传入服务器 注意:指定端口用-P命令 p大写 本机地址 用户名@服务器I ...
- ecshop 除去版权信息
ECSHOP 2.73彻底去版权的方法 前台部分: 1. 去掉头部TITLE “- Powered by ecshop” 后者打开includes/lib_main.php $page_title = ...
- WWDC2017-whats_new_in_safari_view_controller
最后更新: 2017-08-08 官方地址: https://developer.apple.com/videos/play/wwdc2017/225/ WWDC2017中,对SafariViewCo ...
- Hyperledger交易流程
Hyperledger Fabric Network中的角色 在Hyperledger中,由三种类型的角色: Client:应用客户端,用于将终端用户的交易请求发送到区块链网络: Peers:负责维护 ...
- mysql5.6.36 源码安装过程
参考:http://www.linuxidc.com/Linux/2015-06/119354.htm cmake编译时,提示错误 CMake Error: The source directory ...
- Android流媒体开发之路三:基于NDK开发Android平台RTSP播放器
基于NDK开发Android平台RTSP播放器 最近做了不少android端的开发,有推流.播放.直播.对讲等各种应用,做了RTMP.RTSP.HTTP-FLV.自定义等各种协议,还是有不少收获和心得 ...
- JS - 创建只读属性
一:为私有变量创建get()方法 这种方式可以创建 "伪 "只读属性.这并不是一种好方法,因为使用_函数_获得只读的_属性_不太符合一般的逻辑. /** * Represent a ...
- SpringBoot 集成mongodb(2)多数据源配置
github:https://github.com/xiaozhuanfeng/mongoProj 现MongoDB有两个数据库: pom.xml: <!-- mongodb 配置 --> ...