hdu 4466 Triangle (12年成都区域现场赛c题)(dp+组合数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4466

Triangle

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 227    Accepted Submission(s): 115


Problem 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


 

Source


 

Recommend
liuyiding

 

题意:

给你一段长度为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;

 

代码:

/*
  注释:把djw[]看成因子fact[]可能会方便你理解。哈哈!
  1.本质三角形就是三边互质的三角形
  2.dp[n]表示以n为周长的三角形个数
  3.djw[i]表示将i个三角形用插隔板法为djw[i]=2^(i-1)
  4.如果觉得插隔板法不理解的话,你也可以理解为i个单位周长的三角形取或者不取,
    至少取一个.那么就有2^(i-1)种组合情况。
  5.c=(c+(__int64)dp[i]*djw[n/i])%mod;这里计算sum(dp[i] * djw[n/i])。i为n的因子
    周长为i的三角形有n/i个,各种不同的组合形成不同的三角形。
  6.主函数中的优化:n很大,到了10^6(利用对称的特点:n/i=j,则对称的可以得到n/j=i)
*/


#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;

const int mod=(int)1e9+7;
const int N=5e6+10;
int dp[N],djw[N];

void cal_bztri()     //计算本质三角形的个数
{
    int i,j;
    dp[3]=1;                     
    for(i=4;i<N;i++)             //动规方程求周长在1—N范围内的三角形 
    {
        dp[i]=dp[i-1]+floor((i-1)/2.)-ceil(i/3.)+1;
        if(!(i&1)) dp[i]-=floor((i/2)/2.);
        if(dp[i]>=mod) dp[i]-=mod;
        if(dp[i]<0) dp[i]+=mod;
    }
    djw[1]=1,djw[2]=2;
    for(i=3;i<N;i++)
    {
        djw[i]=djw[i-1]*2;       //递推(每次多一个就多了一次取与不取的情况)
        if(djw[i]>=mod) djw[i]-=mod;
        for(j=2;i*j<N;j++)         //去掉那些可以约分的三角形,因为如果能约分,就不满足互质的条件(筛选)
        {							//比如3*(1,1,1)就是一个可约分三角形
            int t=i*j;
            dp[t]-=dp[i];
            if(dp[t]<0) dp[t]+=mod;
        }
    }
}

int main()
{
    int cases=0,n;
    cal_bztri();
    while(scanf("%d",&n)!=EOF)
    {
        __int64 c=0;
        for(int i=1;i*i<=n;i++)     //优化
        {
            if(n%i==0)   //计算sum(dp[i] * djw[n/i]) i为n的因子
            {
                c=(c+(__int64)dp[i]*djw[n/i])%mod;
                if(i*i!=n)           //如果n是平方数就只需要算一次
                    c=(c+(__int64)dp[n/i]*djw[i])%mod;
            }
        }
        printf("Case %d: %I64d\n",++cases,(c+mod)%mod);
    }
    return 0;
}


/*
  优化部分也可写成:
  for(int i=1;i<=n;i++) 
     if(n%i==0) 
        c=(c+(__int64)dp[i]*djw[n/i])%MO;
   //其实可以写成n/2,不过还是没有sqrt(n)优化的好。
*/


 

8674165

2013-07-23 09:24:46

Accepted

4466

1796MS

39420K

1109 B

C++

 

 

哈哈哈!!!! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值