(POJ1837)Balance(附力矩相关讲解)

添加链接描述
题目
Gigel has a strange “balance” and he wants to poise it. Actually, the device is different from any other ordinary balance.
It orders two arms of negligible weight and each arm’s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1…25. Gigel may droop any weight of any hook but he is forced to use all the weights.
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.

Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device.
It is guaranteed that will exist at least one solution for each test case at the evaluation.

题解:
我发现其他博客题解已经写的很帮了,但因为力矩这东西要大学才学,有很多高中和初中,小学,乃至幼儿园的同学没有接触过力矩,这里我把力矩讲一下在这里插入图片描述
如图,我相信图片比语言来的直接

题目大意:
题意:
有一个device(直杆),以中点为支点,左右长度都为15,上面在不同位置分布着一些挂钩。现在给出c个挂钩,和它们在杆上的位置(以中点的原点的x轴表示),以及Gigel有G个砝码,和它们各自的重量。求如果把这些钩码全部挂在任意钩上(一钩个挂任意多个),最终device能达到平衡的情况数。

分析:
这是一题杠杆原理的物理题,要使杠杆平衡这左边的负力矩等于右边的正力矩。即所有的力矩之和为0.
所以我们要求的是:将所有的砝码放上去后力矩和为0的方案数?
每个砝码可以放置在任意的位置上,只要最后力矩和为0即可。那么我们就要枚举出所有的情况,然后看最后和为0的方案有多少。
力矩有负数的情况所以为了方便表示,我们将所有的情况向右移7500位,力矩和的范围[0,15000].就变成了一道类似01背包的问题了。
dp[i][j]表示放了前i个砝码后,力矩和为j的方案数。
所以状态转移方程: dp[i][j + w[i]*h[k] ] += dp[i][j]
初始化:刚开始是平衡的所以 dp[0][7500] = 1;

题目大意来自这片博客

如果学过dp,这就是道dp题吧,只不过因为负值无法表示,我们要把一个正数看做0;
代码如下:

#include<iostream>
#include<cmath>
using namespace std;
int dp[25][15010];//表示前i个物体 力矩为j的方案数
int h[25],w[25];//表示钩子的位置,和重量
int x,y;
int main()
{cin>>x>>y;
int min=0,max=0;
 for(int i=1;i<=x;i++)
    {cin>>h[i];

    }
    int sum=0;
 for(int i=1;i<=y;i++)
    {cin>>w[i];

    }

    dp[0][7000]=1;
    int mid=7000;
 for(int i=1;i<=y;i++)
    for(int j=0;j<=15000;j++)
        if(dp[i-1][j])
        for(int k=1;k<=x;k++)
            dp[i][j+w[i]*h[k]]+=dp[i-1][j];







        cout<<dp[y][7000]<<endl;


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值