题目链接: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:

 ///欧拉公式+费马小定理求逆元
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#define INF 0x3f3f3f3f
#define ll long long int
#define mod 1000000007
using namespace std; ll N, V, E;
ll quick_pow(ll a, ll b)
{
ll ans = ;
while(b)
{
if(b&) ans = ans*a%mod;
a = a*a%mod;
b>>=;
}
return ans;
}
int main()
{
scanf("%lld", &N);
V = N*(N-)%mod*(N-)%mod*(N-)%mod*quick_pow(,mod-)%mod; ///内部交点
E = (V* + (N*(N-)/)%mod)%mod; ///边数
V = (V + N)%mod; ///顶点数
ll F = ((E-V+)%mod+mod)%mod; ///面数
printf("%lld\n", F);
return ;
}

(第八场)G Counting regions 【欧拉公式】的更多相关文章

  1. 牛客多校训练第八场G.Gemstones(栈模拟)

    题目传送门 题意: 输入一段字符串,字符串中连续的三个相同的字符可以消去,消去后剩下的左右两段字符串拼接,求最多可消去次数. 输入:ATCCCTTG   输出:2 ATCCCTTG(消去CCC)——& ...

  2. 牛客多校第八场 G Gemstones 栈/贪心

    题意: 对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组. 题解: 直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈 #include<bits/stdc++.h> usin ...

  3. [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G

    [HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...

  4. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  5. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  6. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

  7. ACM-ICPC 2017 Asia Urumqi(第八场)

    A. Coins Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all wit ...

  8. 牛客多校第四场 G Maximum Mode

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  9. 牛客多校第二场 G transform

    链接:https://www.nowcoder.com/acm/contest/140/G White Cloud placed n containers in sequence on a axes. ...

随机推荐

  1. Js简易代码生成工具

    代码 javascript:(function(){ document.body.innerHTML = '<textarea id="txtTemplate" style= ...

  2. TOJ 4523 Transportation

    Description Given N stations, you want to carry goods from station 1 to station N. Among these stati ...

  3. 最强json解析工具

    [原]http://blog.csdn.net/xiaoguomumu/article/details/75255629 感觉上面的链接所说,需要传一个T进去,也就是先要构造T,感觉麻烦 可以不这样做 ...

  4. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  5. JavaScript对象 原型

    javascript对象就是一组数据和功能的集合,除原始类型(string.number.boolean.null.undefined)之外,其余都是对象. 可以通过对象直接量(字面量).new.和O ...

  6. Android界面编程--使用活动条(ActionBar)--通过ActionBar菜单改变TextView的字体和颜色

    android:orientation="vertical"(AndroidStudio不提示,这个要记住了) 昨天好不容易把ActionBar从溢出菜单overflow中弄出来了 ...

  7. kafka自定义序列化器

    <kafka权威指南> Customer.java public class Customer { private int customId; private String custome ...

  8. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...

  9. Nginx管理(一)

    一.Nginx介绍 Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务. 1.Nginx历史和特性 Nginx是由伊戈尔·赛索耶夫为俄罗斯 ...

  10. 可编辑DIV 光标位置 处理

    //场景: 要做一个网页即时通信,发送信息的文本编辑框 要求能发图片和表情,那么textarea就不能满足需求了,因为textarea内没有办法加入image // 采用方案是使用可编辑的DIV(也就 ...