zoukankan      html  css  js  c++  java
  • hdu-5621 KK's Point(dp+数学)

    题目链接:

    KK's Point

    Time Limit: 2000/1000 MS (Java/Others)   

     Memory Limit: 65536/65536 K (Java/Others)


    Problem Description
     
    Our lovely KK has a difficult mathematical problem:He points N(2N10^5) points on a circle,there are all different.Now he's going to connect the N points with each other(There are no three lines in the circle to hand over a point.).KK wants to know how many points are there in the picture(Including the dots of boundary).
     
    Input
     
    The first line of the input file contains an integer T(1T10), which indicates the number of test cases.

    For each test case, there are one lines,includes a integer N(2N10^5),indicating the number of dots of the polygon.
     
    Output
     
    For each test case, there are one lines,includes a integer,indicating the number of the dots.
     
    Sample Input
    2
    3
    4
     
    Sample Output
    3
    5
     
    题意:
     
    在圆上有n个点,每两个点都相连,问连线有多少个交点,任意三根线不交于同一点;
     
    思路
     
    dp来解决,每加入一个点我们可以发现,增加的交点个数为1*n+2*(n-1)+3*(n-2)+...+n*1+1(最后这个1是这个点本身);
    这是怎么来的可以画个图来自己画一下看看;每次把原先的点分成两拨;
     
    现在的问题变成怎么求上述的式子了;
     
    f(n)=1*n+2*(n-1)+3*(n-2)+...+n*1;
    f(n+1)=1*(n+1)+2*n+3*(n-1)+...+(n+1)*1;
    f(n+1)-f(n)=(n+1)+n+(n-1)+(n-2)+...+1=(n+1)(n+2)/2;
    那么状态转移方程就有了dp[i]=dp[i-1]+1+f(i-3);
     
     
    AC代码
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    using namespace std;
    typedef long long ll;
    const ll mod=1e9+7;
    const ll inf=1e15;
    const int N=1e5+6;
    int n;
    ll dp[N],f[N];
    void fun()
    {
        dp[1]=1;//注意dp[1]   dp[2]的初始化,我在这里就wa了一发;
        dp[2]=2;
        dp[3]=3;
        f[1]=1;
        for(int i=2;i<N;i++)
        {
            ll x=(i);
            f[i]=f[i-1]+x*(x+1)/2;
        }
        for(int i=4;i<N;i++)
        {
            dp[i]=dp[i-1]+1+f[i-3];
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        fun();
        while(t--)
        {
            scanf("%d",&n);
            cout<<dp[n]<<"
    ";
            //printf("%lld
    ",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    java 大数据处理类 BigDecimal 解析
    关于纠正 C/C++ 之前在函输内改变 变量的一个错误想法。
    C++ 制作 json 数据 并 传送给服务端(Server) 的 php
    介绍一个很爽的 php 字符串特定检索函数---strpos()
    如何 判断 设备 是否 连接 上 了 wifi
    android 通过访问 php 接受 or 传送数据
    正则匹配抓取input 隐藏输入项和 <td>标签内的内容
    手把手教你Chrome扩展开发:本地存储篇
    HTML5之本地存储localstorage
    初尝CDN:什么是分布式服务节点?
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5428730.html
Copyright © 2011-2022 走看看