HDU1396:Counting Triangles -DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1396
//根据每增加一条边所增加的正三角和倒三角的个数构造方程
#include<cstdio>
#include<cstring>
__int64 dp[510];
int main()
{
	int n;
	memset(dp,0,sizeof(dp));
    dp[1]=1;
	for(int i=2;i<=500;i++)
	{
		if(i&2==1)
		{
			dp[i]=dp[i-1]+(i*i-1)/4+i*(i+1)/2;
		}
		else
		{
			dp[i]=dp[i-1]+(i*i)/4+i*(i+1)/2;
		}
	}
	while(~scanf("%d",&n))
	{
		printf("%I64d\n",dp[n]);
	}
	return 0;
 } 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-27 12:59:28

HDU1396:Counting Triangles -DP的相关文章

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

UVA 12075 - Counting Triangles(容斥原理计数)

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

LightOJ 1307 Counting Triangles 二分查找

[题解整理]二分题 题目类型: 二分查找: 二分答案. 大致解题思路: 查找注意有序和返回值: 浮点数注意精度: 整数注意返回值,建议另外维护一个变量,用于储存可行解. 题目 分类 传送门 WA点 poj 2785 二分查找 题解 lightoj 1088 二分查找 题解 lightoj 1307 二分查找 题解 longlong poj 2456 整数二分答案 题解 poj 3104 整数二分答案 题解 poj 3258 整数二分答案 题解 poj 3273 整数二分答案 题解 lightoj

HDU 1260:Tickets(DP)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 923    Accepted Submission(s): 467 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

HDU1158:Employment Planning(DP)

Problem Description A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a work

POJ 2192 :Zipper(DP)

恢复 DP一直以来都没怎么看,觉得很难,也只会抄抄模板和处理一些简单的背包.于是比赛出现了这道比较简单的题还是不会,要开始去学学DP了. 题意:给出3个字符串,分别为A串和B串还有C串,题目保证A的长度+B的长度=C的长度,C里面包含着A和B的字符,然后判断A和B在C里面的字符顺序还是不是和原来的A和B保持原样. 查看题目 做法:建立一个DP数组dp[i][j],第一维i表示使用了A的前i个字符,第二维j表示使用了B的前j个字符,然后赋予1或者0判断是否可以组成.如果此时c[i+j]==a[i]

二:状压dp

一:状压dp的基本特征 状态压缩问题一般是指用十进制的数来表示二进制下的状态 这种用一个数来表示一组数,以降低表示状态所需的维数的解题手段,就叫做状态压缩. 常用到位运算 二:位运算 &:与运算,相同为1不同为0 | :或运算,全0位0,否则为1 ^:异或运算,不同为1,相同为0(也叫半加与运算) <<:左移操作,x<<j,x在二进制下向左移j位,(也就是原来的数乘j即:x*=j)右边用0填充,反码不同,要用1填充(负数) >>:右移操作,x>>j相