hdu 3199 丑数
题意:将素因子只有p1,p2,p3的数排序,求第n个数是多少
拿小数乘p1,p2,p3不断扩展得到整个序列。比较棒的一个思路是用l1来标记p1应该由序列里第几个数来扩展了
用优先权队列的话不好控制重复入队的数
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//1e18 2^60 开200的时候T ??
typedef long long LL;
LL dp[10000];
int main()
{
int p1,p2,p3,idx;
while(scanf("%d%d%d%d",&p1,&p2,&p3,&idx)==4) //没写while wa了
{
dp[0]=1;
int l1,l2,l3;
l1=l2=l3=0;
for(int i=1;i<=idx;++i)
{
dp[i]=min(min(dp[l1]*p1,dp[l2]*p2),dp[l3]*p3);
if(!(dp[i]%p1)) ++l1;
if(!(dp[i]%p2)) ++l2;
if(!(dp[i]%p3)) ++l3;
}
printf("%lld\n",dp[idx]);
}
return 0;
}
hdu 1058
题意:素因子数只含有2 3 5 7的数为humble number,要求输出第n个humble number
输出比较奇怪
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[5900];
int main()
{
dp[0]=0;
dp[1]=1;
int l1,l2,l3,l4;
l1=l2=l3=l4=1;
for(int i=2;i<=5842;++i)
{
dp[i]=min(min(dp[l1]*2,dp[l2]*3),min(dp[l3]*5,dp[l4]*7));
if(!(dp[i]%2)) ++l1;
if(!(dp[i]%3)) ++l2;
if(!(dp[i]%5)) ++l3;
if(!(dp[i]%7)) ++l4;
}
int n;
while(scanf("%d",&n)!=EOF&&n)
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %d.\n",n,dp[n]);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %d.\n",n,dp[n]);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %d.\n",n,dp[n]);
else
printf("The %dth humble number is %d.\n",n,dp[n]);
}
return 0;
}