Letters比赛第六场1004 Unit Fraction Partition解题报告

1004 Unit Fraction Partition(POJ 1980)

解题思路:DFS + 剪枝。这题的剪枝条件还是比较严格的,很容易超时,我想到的需要剪枝的情况有以下几点:①前几项的和超过了最大值。②前几项的积超过了最大值。③深度超出。④提前“预测”剪枝:即如果剩余的项数乘以当前最小分数要大于剩余的值,则不应该往下搜索。第④点是至关重要的,没有考虑到的话一般会超时。还有可以优化的地方就是避免用实数类型,一是精度难以把握,而是实数运算相对整数运算要慢。

 

代码如下:

 

#include <cstdlib>
#include <iostream>

using namespace std;

int p = 0;
int q = 0;
int a = 0;
int n = 0;
int result = 0;

void DFS(int last, int pt, int qt, int times)
{
    if(times == n+1)
{ return; } int ptt = (a/qt+1)*pt+qt; int qtt = (a/qt+1)*qt; for(int i=a/qt; i>=last; i--) { ptt -= pt; qtt -= qt; if(q*ptt == p*qtt) { result++; return; } else if(q*ptt < p*qtt) { if(ptt*i*q + (n-times)*qtt*q >= p*qtt*i)
{ DFS(i, ptt, qtt, times+1); } } else { return; } } } int main() { #ifdef MYLOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif while(scanf("%d %d %d %d", &p, &q, &a, &n) != EOF && p+q+a+n) { result = 0; DFS(1, 0, 1, 1); printf("%d\n", result); } return 0; }

 

 

转载于:https://www.cnblogs.com/LETTers/archive/2012/04/24/2469023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android resource fraction refers to the ability to specify resource values as fractions or percentages of a parent resource. This is commonly used in Android development to create responsive layouts that adapt to different screen sizes and resolutions. For example, instead of specifying a fixed width or height for a view, you can use resource fractions to define the size relative to the parent container. This allows the view to scale proportionally when the container's size changes. To use resource fractions in Android, you can define them in XML resource files using the "fraction" unit. For instance, you can set the width of a view to half of the parent container by using a fraction value of "0.5". Here's an example of how you can use resource fractions in an XML layout file: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/blue" /> </LinearLayout> ``` In this example, two views are placed inside a LinearLayout with a vertical orientation. Both views have a width defined as half of the parent container, using the "@fraction/0.5" resource value. Using resource fractions can help create more flexible and responsive UI designs in Android applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值