C. Polygon for the Angle
time limit per test 2 seconds
memory limit per test 256 megabytes
Problem Decription
You are given an angle ang.The Jury asks You to find such regular n-gon (regular polygon with n vertices) that it has three vertices a, b and c (they can be non-consecutive) with ∠abc=ang or report that there is no such n-gon.
If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn’t exceed 998244353.
Input
The first line contains single integer T
(1≤T≤180) — the number of queries.
Each of the next T lines contains one integer ang (1≤ang<180) — the angle measured in degrees.
Output
For each query print single integer n(3≤n≤998244353) — minimal possible number of vertices in the regular n-gon or −1 if there is no such n.
Example
Input
4
54
50
2
178
Output
10
18
90
180
Note
The answer for the first query is on the picture above.
The answer for the second query is reached on a regular 18-gon. For example, ∠v2v1v6=50∘.
The example angle for the third query is ∠v1v0v2=2.
In the fourth query, minimal possible n is 180 (not 90).
解题心得:
- 很简单的一个计算几何,初看起来有点蒙,但是在这个正多边形的外面添加一个外接圆就很简单了。
- 在这里主要需要得到两个角,多边形的一个内角,以及一个最小弧对应的角。根据等弧对等角可以得知,任选三点得到的角一定是最小角的倍数。一个内角如图就是<Y的二倍,2 × <Y = 180 - 360/n 这样可以得到一个内角。然后(假设图上对应的是一个最小弧)最小角就是<R/2,即 <Y/2 = 180 - 内角 / 2。
- 最后在打表的时候要注意任选三点得到的一个角不能大于其内角,就是样例的最后一个。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
struct Ang {
double a, b;
}ang[maxn];
void pre_deal() {
ang[3].b = ang[3].a = 60;
for(int i=4;i<=360;i++) {
ang[i].a = 180 - (double)360/i;
ang[i].b = (double)(180 - ang[i].a)/2.0;
}
}
int query(int x) {
for(int i=3;i<=360;i++) {
for(double j=ang[i].b;j<=360 && j<= ang[i].a; j+= ang[i].b) {
if(j == x) return i;
}
}
return -1;
}
int main() {
//freopen("1.in", "r", stdin);
pre_deal();
int t; scanf("%d", &t);
while(t--) {
int que; scanf("%d", &que);
printf("%d\n", query(que));
}
}