zoukankan      html  css  js  c++  java
  • (第八场)G Counting regions 【欧拉公式】

    题目链接:https://www.nowcoder.com/acm/contest/146/G

    G、Counting regions

    | 时间限制:1 秒 | 内存限制:128M
    Niuniu likes mathematics. He also likes drawing pictures. One day, he was trying to draw a regular polygon with n vertices. He connected every pair of the vertices by a straight line as well. He counted the number of regions inside the polygon after he completed his picture. He was wondering how to calculate the number of regions without the picture. Can you calculate the number of regions modulo 1000000007? It is guaranteed that n is odd.

    输入描述:

    The only line contains one odd number n(3 ≤ n ≤ 1000000000), which is the number of vertices.

    输出描述:

    Print a single line with one number, which is the answer modulo 1000000007.

    备注: The following picture shows the picture which is drawn by Niuniu when n=5. Note that no more than three diagonals share a point when n is odd.

    示例 1

    输入

    3

    输出

    1

    示例2

    输入

    5

    输出

    11

    题意概括:

    给你一个正n边形,将n个顶点两两连边,问内部有多少个区域。n是奇数。

    官方题解:

    欧拉公式:F=E-V+2
    内部交点个数:C(n,4)
    一条线段会被一个交点分成两段,所以x条直线的交点会多分出来x条线段,利用V 可以算出E。

    解题思路:

    根据欧拉公式:F:面数;E:边数;V:顶点数

    当V >= 4时,多边形满足 V-E+F = 2;

    因为N是奇数,所以任意选取四个顶点连接,两条对角线相交于一点,交点两两不重合,所以内部交点个数为 C(n, 4);

    而内部边数 = 对角线数 + 内部交点数*2 (即 C(n, 2) + C(n, 4)*2);

    最后边数,结点数已知,可求面数。

    注意细节:

    求组合时数据偏大有取模操作,除法运算要通过逆元转换为乘法,这里求逆元的方法是用费马小定理。

    AC code:

     1 ///欧拉公式+费马小定理求逆元
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <cmath>
     6 #include <cstring>
     7 #define INF 0x3f3f3f3f
     8 #define ll long long int
     9 #define mod 1000000007
    10 using namespace std;
    11 
    12 ll N, V, E;
    13 ll quick_pow(ll a, ll b)
    14 {
    15     ll ans = 1;
    16     while(b)
    17     {
    18         if(b&1) ans = ans*a%mod;
    19         a = a*a%mod;
    20         b>>=1;
    21     }
    22     return ans;
    23 }
    24 int main()
    25 {
    26     scanf("%lld", &N);
    27     V = N*(N-1)%mod*(N-2)%mod*(N-3)%mod*quick_pow(24,mod-2)%mod;  ///内部交点
    28     E = (V*2 + (N*(N-1)/2)%mod)%mod;    ///边数
    29     V = (V + N)%mod;  ///顶点数
    30     ll F = ((E-V+1)%mod+mod)%mod;  ///面数
    31     printf("%lld
    ", F);
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    用UIScrollView产生视差效果
    梦幻星空动画
    固定UIScrollView滑动的方向
    关于UIScrollView有些你很难知晓的崩溃情形
    使用一元二次方程做实时动画
    RDMBorderedButton
    如何查看开发者账号何时到期
    [翻译] TGLStackedViewController
    【转】Tomcat配置文件入门
    Servlet 工作原理解析
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9464013.html
Copyright © 2011-2022 走看看