披萨分割

2017-03-26  本文已影响0人  Gitfan

题目描述
METO 喜欢吃披萨,同时他又是一个对几何美学颇有研究的人。



对于一个披萨,METO 首先会在边缘随机取 n
n 个不重复的点,然后每两点连一条线。
METO 想知道沿着这些线切披萨最多可以将其分为几份?
输入格式
第一行为数据组数 T,(1 \le T \le 100)
T(1≤T≤100)接下去 T
T 行,每行一个正整数 n,(1 \le n \le 10^{9})
n(1≤n≤10​9​​)
输出格式
对每组数据输出一行 ans
ans,表示能分的份数,答案对 10^9+7
10​9​​+7 取模
样例数据
输入
512345
输出
124816

题解:由欧拉定理得:V+F-E=2,V为顶点个数,F为平面数目,E为边的数目。容易知道,在多边形内的交点的个数为C(n,4),因为每四个点形成一个交点。所以V=n+C(n,4)。因为完全图的边数为C(n,2),还要加上圆边上的n条边。又因为多边形内部的边的交点会导致边的数目的增加,而在遍历多边形内部的边时,因为交点都是由两条边相交形成的,所以那些遍历那些边的时候,交点都会被访问两次,也就是新增的边数为2C(n,4)。所以边数一共为n+C(n,2)+2C(n,4),推出F为C(n,2)+C(n,4)+2;而答案不包括圆外部的那个面,所以答案为C(n,2)+C(n,4)+1.

https://scut.online/problem.php?id=72

/*
根据费马小定理:

已知(a, p) = 1,则 a^(p-1) ≡ 1 (mod p),  所以 a*a^(p-2) ≡ 1 (mod p)。

也就是 (m!)的取模逆元为 (m!)^p-2 ;
*/
#include <cstdio>
#include<algorithm>
#include <stdlib.h>
#include<string.h>
#define maxn 1000010
using namespace std;
typedef long long LL;
const LL mod=1000000007;
LL quick(LL a,LL b)
{
    LL res=1,base=a;
    while(b)
    {
        if(b&1) res=(res*base)%mod;
        b>>=1;
        base=(base*base)%mod;
    }
    return res;
}
LL combine(LL n,LL m)
{
    if(n<m) return 0;
    LL ans=1,ca=1,cb=1;
    for(LL i=0;i<m;i++)
    {
        ca=(ca*(n-i))%mod;
        cb=(cb*(m-i))%mod;
    }
    ans=(ca*quick(cb,mod-2))%mod;
    return ans;
}
LL Lucas(LL n,LL m)
{
    if(m==0) return 1;
    return combine(n%mod,m%mod)*Lucas(n/mod,m/mod);
}
int main()
{
    int t;
    scanf("%d",&t);
    LL n,res;
    while(t--)
    {
        scanf("%lld",&n);
        res=(Lucas(n,2)+Lucas(n,4)+1)%mod;
        printf("%lld\n",res);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读