HDU - 4466 Triangle

Description

You have a piece of iron wire with length of n unit. Now you decide to cut it into several ordered pieces and fold each piece into a triangle satisfying:

*All triangles are integral.

* All triangles are pairwise similar.

You should count the number of different approaches to form triangles. Two approaches are considered different if either of the following conditions is satisfied:

*They produce different numbers of triangles.

* There exists i that the i th (again, pieces are ordered) triangle in one approaches is not congruent to i th triangle in another plan.

The following information can be helpful in understanding this problem.

* A triangle is integral when all sides are integer.

*Two triangles are congruent when all corresponding sides and interior angles are equal.

* Two triangles are similar if they have the same shape, but can be different sizes.

*For n = 9 you have 6 different approaches to do so, namely

(1, 1, 1) (1, 1, 1) (1, 1, 1)

(1, 1, 1) (2, 2, 2)

(2, 2, 2) (1, 1, 1)

(1, 4, 4)

(2, 3, 4)

(3, 3, 3)

where (a, b, c) represents a triangle with three sides a, b, c.

Input

There are several test cases.

For each test case there is a single line containing one integer n (1 ≤ n ≤ 5 * 10 6).

Input is terminated by EOF.

Output

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the number of approaches, moduled by 10 9 + 7.

Sample Input

 1
2
3
4
5
6
8
9
10
11
12
15
19
20
100
1000 

Sample Output

 Case 1: 0
Case 2: 0
Case 3: 1
Case 4: 0
Case 5: 1
Case 6: 2
Case 7: 1
Case 8: 6
Case 9: 3
Case 10: 4
Case 11: 10
Case 12: 25
Case 13: 10
Case 14: 16
Case 15: 525236
Case 16: 523080925 

题意:给定一个长度为n的铁丝,将其分成有顺序的若干份,每份折成三角形,要求所有的三角形相似。三角形顺序不同视为不同方案,三边相等视为同一方案。求方案个数。

思路:参考了:

1、本质三角形的定义:设a、b、c为三角形的三边,则gcd(a,b,c)=1。

2、先不考虑边互不互质,算出周长为i的三角形个数(动规实现,其实暴力应该也可以)。然后算周长为i,即

i个单位长度作边长的三角形的组合数。

a)你用隔板法考虑就是i个点,i-1个空格插隔板,djw[i]=2^(i-1)种;

b)你也可以按选与不选考虑。

注:放在一个格子里的三角形边长合并组成大边长的三角形。

最后就是n/i个周长为i的三角形组合数即为所求。

3、说一下用动态规划求dp[i]的过程:

函数dp(x),表示周长为 x的不同三角形(a,b,c)的数量,我们假设(
a <= b <= c )

  则我们通过枚举最大周长 x, 假设其最大边为 c.

  则问题可以划分为两类独立: 1: b = c  2:b != c

  第一种情况: b = c, 则周长为x的三角形 (a,c,c) 的方案数为:

    因为 a+c+c = x , 且 a <= c 那么

c最大取 A=floor((x-1)/2 ), c最小取 B=ceil( x/3 ), 则此时三角形种类:
A-B+1

  第二种情况: b != c, 则周长为x的三角形 (a,b,c) 的方案数为:

    因为 a+b+c = x, 且 b <= c-1,a+b > c,

    这里, 我们转而考虑 , 形式如 (
a, b, c-1 ) 的三角形, 其方案数为 f( x-1 ),

因为 b <= c-1, 又 a+b > c-1,则当 a+b
> c ,则其就是三角形 (a,b,c)下的不同三角形数量 f(x)了.

但是这里有个地方不满足, 就是当 a+b == c,的时候, 假设其为 M
, 则我们就可以得出: f( x ) = f( x-1 ) - M , (b!=c).

    问题就是如何计算出这个M, 我们继续考虑.

a+b+(c-1) = x-1,

=>a+b+c = x, 又因为 a+b = c. 可以得到

=> c+c = x , 2*c = x, 所以 c = x/2, 因为c为整数,所以我们知道,只有当x为偶数时才会出现这个M.

    又 a + b = c = x/2, 且 a <= b , 此时 a 的取值为 [
1, floor( (x/2)/2 ) ]

    所以当 x为偶数时, M = floor( (x/2)/2 )

当 x为奇数时, M = 0;

dp[i]表示周长为i,那么就有n / i 个这样的三角形。用隔板板,将三角形合并成大的,保证相似就是2
^ (n / i - 1)种情况

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 5000005;
const int mod = 1000000007;

int dp[maxn], num[maxn];

void init() {
	dp[3] = 1;
	for (int i = 4; i < maxn; i++) {
		dp[i] = dp[i-1] + floor((i-1)/2.0) - ceil(i/3.0) + 1;
		if (!(i & 1))
			dp[i] -= i / 4;
		dp[i] %= mod;
		if (dp[i] < 0) dp[i] += mod;
	}

	num[1] = 1;
	for (int i = 2; i < maxn; i++) {
		num[i] = (num[i-1] << 1) % mod;
		for (int j = 2; i * j < maxn; j++) {
			dp[i * j] -= dp[i];
			if (dp[i * j] < 0)
				dp[i * j] += mod;
		}
	}
}

int main() {
	int n, t, cas = 1;
	init();
	while (scanf("%d", &n) != EOF) {
		ll ans = 0;
		for (int i = 1; i * i <= n; i++) {
			if (n % i) continue;
			ans = (ans + (1ll*dp[i]*num[n/i])) % mod;
			if (i * i != n)
				ans = (ans + (1ll*dp[n/i] * num[i])) % mod;
		}
		printf("Case %d: %lld\n", cas++, ans);
	}
	return 0;
}
时间: 2024-10-26 01:01:45

HDU - 4466 Triangle的相关文章

HDU 4324 Triangle LOVE (拓扑排序)

Triangle LOVE Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility that two people love each other, wh

HDU 4324:Triangle LOVE( 拓扑排序 )

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2271    Accepted Submission(s): 946 Problem Description Recently, scientists find that there is love between any of two people. For

HDU 5914 Triangle(打表——斐波那契数的应用)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whose lengths are 1,2, 3?n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides t

HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))

Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Mr. Frog has n sticks, whose lengths are 1,2, 3?n respectively. Wallice is a bad man,

hdu 4324 Triangle LOVE(拓扑排序)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3858    Accepted Submission(s): 1516 Problem Description Recently, scientists f

HDU 4324 Triangle LOVE(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324 题目: Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possi

hdu 4324 Triangle LOVE(拓扑判环)

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3603    Accepted Submission(s): 1416 Problem Description Recently, scientists find that there is love between any of two people. Fo

hdu 5914 Triangle

题意:1 2 ....n,这些边中最少抽出多少条边,使得剩下的边无法组成三角形 分析:开始的时候有点zz,IDA*跑了一发,2^(n-2)*n^3,跑了5min出了17,剩下的怎么都出不来了,然后YY了一下n和ans[n]之间的差,就过了,后来想了一下使得剩下的边都不能组成三角形,只要剩下的都是Fibonacci序列中的数就行了,想想IDA*简直就s是zz啊 #include<bits/stdc++.h> using namespace std; int dp[25]; int main(){

hdu 4324 Triangle LOVE

本题链接:点击打开链接 本题大意: 题意分析(转载):此题可以一遍拓扑排序判环求解 即只需要找到一个环,就必定存在三元环 证明如下: 假设存在一个n元环,因为a->b有边,b->a必定没边,反之也成立所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,这样就形成了一个n-1元环....所以只需证明n大于3时一定有三元环即可,显然成立. 具体请参见代码: #include<std