【转】HDU 3199 Hamming Problem: 规律打表

应该不是数论···逻辑思维?找规律?暂且放到支个分类下···

我的理解:

数组 num表 保存 只有P1 P2 or P3的因子的 常数 并按递增顺序。

通过已有的常数 每次循环相应的乘以P1 P2 P3, 就保证了表中的 常数 的因子排他性。

也可以 手推+程序显示 探究其规律。

#include<iostream>
using namespace std;
const int Size=100000;
long long num[Size];
int main()
{
        long long p1, p2, p3, i;
        while(cin>>p1>>p2>>p3>>i)
        {
                int k1, k2, k3;
                k1=k2=k3=0;
                num[0]=1;
                for(int j=1; j<i+1; j++)
                {
                        long long x1=num[k1]*p1;
                        long long x2=num[k2]*p2;
                        long long x3=num[k3]*p3;
                        long long Min=min(x1, x2);
                        Min=min(Min, x3);
                        num[j]=Min;
                        if(Min==x1) k1++;
                        if(Min==x2) k2++;
                        if(Min==x3) k3++;
//                        cout<<"_______________________"<<endl<<endl;
//                        cout<<"x1="<<x1<<endl;
//                        cout<<"x2="<<x2<<endl;
//                        cout<<"x3="<<x3<<endl;
//                        cout<<"Min="<<Min<<endl;
//                        cout<<"k1 k2 k3="<<k1<<" "<<k2<<" "<<k3<<endl;
//                        cout<<"_______________________"<<endl<<endl;
                }
                cout<<num[i]<<endl;
        }
}

  

 

posted @ 2015-08-12 16:16  _SunDaSheng  阅读(350)  评论(0编辑  收藏  举报