poj 2545 Hamming Problem

简介:

参考别人的思路写的,别人的这种算法很给力!

效率很高O(n)的效率


#include <iostream>
#include <stdio.h>

using namespace std;

__int64 ans[1000000];

__int64 getMin(__int64 a,__int64 b,__int64 c)
{
    __int64 rs=0;
    rs=a<b?a:b;
	rs=rs<c?rs:c;
	return rs;
}

int main()
{
	__int64 a,b,c,M;
	scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&M);

    __int64 a2,a3,a5,i,tmp,n;
    a2=a3=a5=0;
    ans[0]=1;
    for(i=1;i<=M;i++)
    {
        tmp=getMin(ans[a2]*a,ans[a3]*b,ans[a5]*c);
        ans[i]=tmp;
        if(tmp==ans[a2]*a)
            ++a2;
        if(tmp==ans[a3]*b)
            ++a3;
        if(tmp==ans[a5]*c)
            ++a5;
    }
    
	printf("%I64d\n",ans[M]);
    return 0;
}


相关文章
|
9月前
|
Java
HDU-2199-Can you solve this equation?
HDU-2199-Can you solve this equation?
52 0
|
9月前
|
Java
HDU-2199-Can you solve this equation
HDU-2199-Can you solve this equation
45 0
UVa1531 - Problem Bee
UVa1531 - Problem Bee
64 0
|
图形学
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
88 0
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
40 0
欧拉计划Problem 5 最小公倍数
欧拉计划Problem 5 最小公倍数
119 0
|
物联网 Go C++
洛谷【2】P1001 A+B Problem
洛谷【2】P1001 A+B Problem
|
数据挖掘
HDOJ 1032(POJ 1207) The 3n + 1 problem
HDOJ 1032(POJ 1207) The 3n + 1 problem
141 0
POJ 1306 Combinations
POJ 1306 Combinations
125 0