原题:
. Polygon for the Angle
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
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, ∠ v 2 v 1 v 6 = 5 0 。 ∠v_2v_1v_6=50^。 ∠v2v1v6=50。.
The example angle for the third query is ∠ v 11 v 10 v 12 = 2 ∘ ∠v_{11}v_{10}v_{12}=2^∘ ∠v11v10v12=2∘
In the fourth query, minimal possible n is 180 (not 90).
中文:
给你一个度数C,让你找出一个正多边形N,在多边形中连接任意三个点所夹角正好是C,现在让你求这个N最小值,如果没有输出-1
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll t,n,tmp;
ll gcd(ll a,ll b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n;
if(n==180)
{
cout<<-1<<endl;
continue;
}
tmp=180;
ll fac = gcd(n,tmp);
n/=fac;
tmp/=fac;
for(ll i=1;;i++)
{
if(tmp-n>=2)
{
cout<<tmp<<endl;
break;
}
tmp*=i;
n*=i;
}
}
return 0;
}
思路:
在纸上算一算可以知道,任意一个正多边形N,能找到的最小夹角是 180 N \frac{180}{N} N180,最大夹角是 180 N × ( N − 2 ) \frac{180}{N}×(N-2) N180×(N−2)
现在给定了一个度数C,设需要 x x x个 180 N \frac{180}{N} N180,有 180 N × x = C \frac{180}{N}×x=C N180×x=C
此时可以通过对 180 C \frac{180}{C} C180约分,得到最小 N x \frac{N}{x} xN
枚举 i i i,直到 N × i N×i N×i与 x × i x×i x×i的差大于2,即满足夹角小于等于 180 N × ( N − 2 ) \frac{180}{N}×(N-2) N180×(N−2)的条件即可得到答案
ps:180度输出-1