(第八场)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. ...
随机推荐
- 第十六章:自定义push notification sound
前面一节已经讲过如何在ionic中集成jpush,这样我们的hybrid app在部署到ios或者android上面的时候,就可以接收通知了.如果不满足系统自带的声音,可以通过一些方式来播放自定义的通 ...
- connection reset by peer, socket write error问题排查
2018-03-15更新:弄明白connection reset产生的原因,见重新分析connection reset by peer, socket write error错误原因 在开发文件上传功 ...
- TOJ 3184 Mine sweeping
描述 I think most of you are using system named of xp or vista or win7.And these system is consist of ...
- CountDownLatch 多线程,等待所有线程结束
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); 构造 ...
- sqlite3在别的目录写文件的问题
今天碰到一个文件,就是sqlite数据不能把db创建在别的目录下.找了好久不得其解.后来换了一个sqlite jar包就好了. 原来我用的是sqlite-nested 内嵌的jar包. 换成这里的包h ...
- tomcat8.5.8遇到的两个问题
压力测试场景,前端nginx反向代理到4个tomcat实例,在其中的一个实例上产生了大量的countDownConnection Incorrect connection count警告 WARNIN ...
- JS获取鼠标位置,兼容IE FF
由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...
- [转]<加密算法c#>——— 3DES加密之ECB模式 和 CBC模式
本文转自:http://www.cnblogs.com/qq278360339/archive/2013/06/05/3119222.html 最近 一个项目.net 要调用JAVA的WEB SERV ...
- JavaScript数组常用操作总结
我们在日常开发过程中,使用到原生 JavaScript的时候,有时候会频繁的对数组进行操作,今天我把工作以来,经常用到的有关 JavaScript数组的方法总结一下,方便日后工作的时候查找使用! 一. ...
- 【学习笔记】Java中生成对象的5中方法
概述:本文介绍以下java五种创建对象的方式: 1.用new语句创建对象,这是最常用的创建对象的方式. 2.使用Class类的newInstance方法 3.运用反射手段,调用java.lang.re ...