牛客网小白赛5——F 圆circle(脑洞+递推)

题意:一个圆上找n个点两两相连,要求划分的块数最多。

https://www.nowcoder.com/acm/contest/135/F(题目链接)

思路:显然,这是一道直线分平面的变形题,首先我们不管相连的情况,每增加一条直线,平面数最多增加为(与之相交直线数+1)

当n=0时,显然块数sum=1。

n=1,sum=f(0) +  1= 2;

n=2,sum=f(1) + (1 + 0)+ (1+(2 - 1 - 1)*(1 - 1)) = 4;

以此类推可以得到:

F(n) = F(n-1)+\sum_{i=1}^{n-1}(1+(n-i+1)*(i-1))

显然这个式子可以化简为:

F(n) = F(n-1)+\sum_{i=0}^{n-1}(ni+2-i^{2}-n)

通过连续自然数的平方和和直接加和的公式可以化简为:

F(n) = F(n-1)+\frac{1}{6}n^{3}-n^{2}+\frac{17}{6}n-2

通过递推得出:

F(n) = \sum_{k=1}^{n}(\frac{1}{6}n^{3}-n^{2}+\frac{17}{6}n-2)+F(0)

又因为F(0) = 1

可以推得:

F(n) = \frac{1}{24}n(n^{3}-6n^{2}+23n-18)+1

最后附上python版代码(好像不用大数,c++unsign long long好像就够了)

# encoding: utf-8 

while True:
    try:
        n = int(input())
        ans = n * n * n * n + 23 * n * n - 6 * n * n * n - 18 * n
        ans = int(ans / 24) + 1
        print(ans)
    except EOFError:
        break;

c++的也附上一个吧

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 1e5 + 7;
const double ex = 1e-8;
typedef unsigned long long ull;
typedef long long ll;
typedef double dl;
int main()
{
    ull n;
    while(cin >> n){
        ull ans = (n * n * n * n - 6 * n * n * n + 23 * n * n - 18 * n) / 24 + 1;
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41791981/article/details/81165003