(第八场)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:
///欧拉公式+费马小定理求逆元
#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 【欧拉公式】的更多相关文章
- 牛客多校训练第八场G.Gemstones(栈模拟)
题目传送门 题意: 输入一段字符串,字符串中连续的三个相同的字符可以消去,消去后剩下的左右两段字符串拼接,求最多可消去次数. 输入:ATCCCTTG 输出:2 ATCCCTTG(消去CCC)——& ...
- 牛客多校第八场 G Gemstones 栈/贪心
题意: 对于一个序列,把可以把连着三个相同的字母拿走,问最多拿走多少组. 题解: 直接模拟栈,三个栈顶元素相同则答案+1,并弹出栈 #include<bits/stdc++.h> usin ...
- [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G
[HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 2020牛客多校第八场K题
__int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...
- 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 ...
- 牛客多校第四场 G Maximum Mode
链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...
- 牛客多校第二场 G transform
链接:https://www.nowcoder.com/acm/contest/140/G White Cloud placed n containers in sequence on a axes. ...
随机推荐
- struts 2 报错Could not find action or resul 常见错误原因分析
1.struts.xml配置的action与我们访问的action或返回的result确实没有匹配导致.这个时候要仔细检查Struts.xml配置文件: 2.我们的action输入正确,返回结果经过无 ...
- ArrayList,LinkList,HashMap
ArrayList底层实现数组,这是ArrayList get()方法的源码,底层是数组 根据下标返回在数组中对应的位置 ,查询快,插入慢 // Positional Access Operation ...
- mavne 工程jsp页面首行报错
在pom.xml文件中加入servlet依赖 <dependency> <groupId>javax.servlet</groupId> <artifactI ...
- postgres formencode.api.Invalid
错误提示: Invalid: expected an int in the IntCol 'geom', got <type 'str'> '010100000007EBFFFC3A611 ...
- gcc链接非标准(non-standard)命名库
标准命名库: -lnamespace 标准链接库以lib开头, 并以so/a结尾. example gcc test.c -o test -L. -lhello 非标准命名库: -l:libname ...
- fabric省略输出
fab -f test_fabric.py start --hide status,running,stdout,user,aborts,warnings,stderr 省略所有输出--hide st ...
- SpringMVC框架下实现分页功能
1.创建实体类Page.java @Entity public class Page { private int totalRecord;// 表示查询后一共得到多少条结果记录 private int ...
- mvc中RedirectToAction()如何传参?
今天在做一个功能的时,使用RedirectToAction()需要从这里传几个参数,从网上查了一下,这样解决.真好. Return RedirectToAction("Index" ...
- spynner解析中文页面,应该显示中文字符的地方都是?的解决方案
这个是底层的QtWebKit相关库里 用的是Qt的QString spynner在将QString转为Python的通用字符串时,没有考虑到中文编码这一块的问题. Python27\Lib\site- ...
- dubbo-admin网页管理控制台
由于近段时间在看dubbo,网上找到的这个war包发布到tomcat报错,故从git(https://github.com/apache/incubator-dubbo-ops)上重新下载编译了版本 ...