【CF1354C1&C2】Polygon Embedding(求解包含正多边形的最小正方形)

C1传送门
C2传送门

  • 题目:
    求能包含 边长为1的正2n边形 的最小正方形,输出正方形的边长。
  • 推导思路:
    为了一般化推导,这里设正多边形的边长为a。
    • 当n为偶数时:
      在这里插入图片描述
      可以发现正方形的边长=对边之间的距离: t a n ( θ ) = t a n ( π − π n 2 ) = a n s 2 a 2 \displaystyle tan(\theta)=tan(\frac{\pi-\frac{\pi}{n}}{2})=\frac{\frac{ans}{2}}{\frac{a}{2}} tan(θ)=tan(2πnπ)=2a2ans a n s = a ∗ t a n ( π − π n 2 ) = a ∗ t a n ( n π − π 2 n ) ans=\displaystyle a*tan(\frac{\pi-\frac{\pi}{n}}{2})=a*tan(\frac{n\pi-\pi}{2n}) ans=atan(2πnπ)=atan(2nnππ)
    • 当n为奇数时:
      在这里插入图片描述
      最终的正方形肯定不和正多边形的边重合,因为图中的两条绿线不等长,这样得到的不是正方形。
      可以画出最终正方形应该是这样的:
      在这里插入图片描述
      对于正2n边形, a 2 = r 2 + r 2 − 2 r r c o s ( π n ) \displaystyle a^2=r^2+r^2-2rrcos(\frac{\pi}{n}) a2=r2+r22rrcos(nπ) r 2 = a 2 2 ( 1 − c o s ( π n ) ) \displaystyle r^2=\frac{a^2}{2(1-cos(\frac{\pi}{n}))} r2=2(1cos(nπ))a2 θ 1 = π n ∗ n 2 \displaystyle \theta_1=\frac{\pi}{n}*\frac{n}{2} θ1=nπ2n θ 2 = π n ∗ n + 1 2 \displaystyle \theta_2=\frac{\pi}{n}*\frac{n+1}{2} θ2=nπ2n+1 L 1 2 = r 2 + r 2 − 2 r 2 c o s ( θ 1 ) = 2 r 2 ( 1 − c o s ( θ 1 ) ) \displaystyle L_1^2=r^2+r^2-2r^2cos(\theta_1)=2r^2(1-cos(\theta_1)) L12=r2+r22r2cos(θ1)=2r2(1cos(θ1)) L 2 2 = r 2 + r 2 − 2 r 2 c o s ( θ 2 ) = 2 r 2 ( 1 − c o s ( θ 2 ) ) \displaystyle L_2^2=r^2+r^2-2r^2cos(\theta_2)=2r^2(1-cos(\theta_2)) L22=r2+r22r2cos(θ2)=2r2(1cos(θ2)) a n s = L 1 2 + L 2 2 = 2 r 2 ( 1 − c o s ( θ 1 ) ) + 2 r 2 ( 1 − c o s ( θ 2 ) ) 2 \displaystyle ans=\frac{L_1}{\sqrt{2}}+\frac{L_2}{\sqrt{2}}=\frac{\sqrt{2r^2(1-cos(\theta_1))}+\sqrt{2r^2(1-cos(\theta_2))}}{\sqrt{2}} ans=2 L1+2 L2=2 2r2(1cos(θ1)) +2r2(1cos(θ2)) 所以最终: a n s = r 2 ( 1 − c o s ( θ 1 ) ) + r 2 ( 1 − c o s ( θ 2 ) ) \displaystyle ans=\sqrt{r^2(1-cos(\theta_1))}+\sqrt{r^2(1-cos(\theta_2))} ans=r2(1cos(θ1)) +r2(1cos(θ2))
      其中 r 2 = a 2 2 ( 1 − c o s ( π n ) ) \displaystyle r^2=\frac{a^2}{2(1-cos(\frac{\pi}{n}))} r2=2(1cos(nπ))a2 θ 1 = π n ∗ n 2 \displaystyle \theta_1=\frac{\pi}{n}*\frac{n}{2} θ1=nπ2n θ 2 = π n ∗ n + 1 2 \displaystyle \theta_2=\frac{\pi}{n}*\frac{n+1}{2} θ2=nπ2n+1
  • ac代码:
    C1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const double pi = acos(-1.0);
int t, n;
int main()
{
   cin >> t;
   while(t--)
   {
       cin >> n;
       printf("%.8f\n", tan((n*pi-pi)/2/n));
   }
   return 0;
}

C2

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
const double pi = acos(-1.0);
int t, n;
int main()
{
   cin >> t;
   while(t--)
   {
       cin >> n;
       double r2 = 1.0/(2-2*cos(pi/n));
       double ang1 = pi/n*(n/2), ang2 = pi/n*((n+1)/2);
       double ans = sqrt(r2*(1-cos(ang1)))+sqrt(r2*(1-cos(ang2)));
       printf("%.8f\n", ans);
   }
   return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值