题目链接:

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(2≤N≤10^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(1≤T≤10), which indicates the number of test cases.

For each test case, there are one lines,includes a integer N(2≤N≤10^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+;
const ll inf=1e15;
const int N=1e5+;
int n;
ll dp[N],f[N];
void fun()
{
dp[]=;//注意dp[1] dp[2]的初始化,我在这里就wa了一发;
dp[]=;
dp[]=;
f[]=;
for(int i=;i<N;i++)
{
ll x=(i);
f[i]=f[i-]+x*(x+)/;
}
for(int i=;i<N;i++)
{
dp[i]=dp[i-]++f[i-];
}
}
int main()
{
int t;
scanf("%d",&t);
fun();
while(t--)
{
scanf("%d",&n);
cout<<dp[n]<<"\n";
//printf("%lld\n",dp[n]);
}
return ;
}

最新文章

  1. Codeforces Round #376A (div2)
  2. jquery EasyUI datagrid 的扩展
  3. 【mysql】关于乐观锁
  4. php搜索分页
  5. svn使用(三)
  6. Android UI系列-----ScrollView和HorizontalScrollView
  7. IOS中表视图(UITableView)使用详解
  8. Spring MVC 解读——@RequestMapping (1)(转)
  9. 动态规划(斜率优化):SPOJ Commando
  10. CSS3新动画效果
  11. VisualStudio 自动排版等 快捷键
  12. CentOS6.5+mysql5.1源码安装过程
  13. windows修改Host后未生效。
  14. poj 3278 简单BFS
  15. docker 使用案例:部署nginx
  16. Deepin 自动挂载win NTFS磁盘
  17. JS正则四个反斜杠的含义
  18. Spring Boot 1.5.10 发布:修复重要安全漏洞!!!
  19. 大数据开发实战:Hive优化实战1-数据倾斜及join无关的优化
  20. Python爬虫实例(三)代理的使用

热门文章

  1. How to Create a Provisioning Profile for iPhone
  2. lstm公式推导
  3. 怎样制作gif图片?怎样制作你项目的动态效果图到你的csdn?
  4. 命令行查看w3wp进程信息
  5. 最简单的基于FFmpeg的移动端样例附件:SDL Android HelloWorld
  6. 网页编程-django前传
  7. iOS开发:Toast for iPhone
  8. 每天一点儿Java--list
  9. PythonCookBook笔记——数据编码和处理
  10. Mvc创建并注册防盗链