Codeforces - 1097B Petr and a Combination Lock

Codeforces - 1097B Petr and a Combination Lock


原题地址
Description
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 n 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.

Input
The first line contains one integer nn (1 ≤ n ≤ 15) — the number of rotations.
Each of the following nn lines contains one integer ai (1 ≤ ai ≤ 180) — the angle of the i-th rotation in degrees.

Output
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).
Examples

InputOutput
3
10
20
30
YES
3
10
10
10
NO
3
120
120
120
YES

Note
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 360 degrees clockwise and the pointer will point at zero again.

思路:
每次旋转有两种状态,即顺时针和逆时针旋转,相应对应正负两个状态。
观察得出旋转次数最多也就15次,即2^15次,不会TLE,于是就想到遍历一遍每个旋转是顺时针还是逆时针,是正还是负。
以上。

AC代码:

#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#include<sstream>
#include<string.h>
#include<cmath>
using namespace std;
int main(){
	int n;
	int t[20],p[20];	//t是数字,p是符号
	cin >> n;
	for(int h = 0;h < n;h ++){
		scanf("%d",&t[h]);
	}
	int i;
	fill(p,p + n,1);
	for(i = 0;i < (1 << n);i ++){	//(1 << n) 即 2^n,1的二进制左移n位
		int k = i,w = 0;
		int sum = 0;
		while(k != 0){
			if(k & 1)p[w ++] = 1;	//按位与计算,就是判断k是否为奇数
			else p[w ++] = -1;
			k >>= 1;
		}
		for(int j = 0;j < n;j ++){
			sum += p[j] * t[j];
		}
		if(sum % 360 == 0)break;
	}
	if(i < (1 << n))printf("YES");
	else printf("NO");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值