多式子递推poj2545 poj2591 poj1338

就是理解选择最小的,并且指针要移动,而是if里面是不能有else的,这样的话就可以保证相同的数字能够同时并进


poj2545


#include<stdio.h>
#include<algorithm>
using namespace std;

int main()
{
long long a[100000];
long long n1,n2,n3;
long long n;
scanf("%lld%lld%lld%lld",&n1,&n2,&n3,&n);

long long p1=0,p2=0,p3=0;
a[0]=1;

for(int i=1;i<=n;i++){
a[i]=min(a[p1]*n1,min(a[p2]*n2,a[p3]*n3));
if(a[i]==a[p1]*n1)
p1++;
if(a[i]==a[p2]*n2)
p2++;
if(a[i]==a[p3]*n3)
p3++;
}

printf("%lld",a[n]);
return 0;
}



poj2591

#include<stdio.h>
#include<algorithm>
using namespace std;

const int MAXX_= 10000005;
int a[MAXX_];

int main()
{
int p1=1,p2=1;
a[1]=1;
for(int i=2;i<=10000000;i++){
a[i]=min(a[p1]*2+1,a[p2]*3+1);
if(a[i]==a[p1]*2+1)
p1++;
if(a[i]==a[p2]*3+1)
p2++;
}

int n;
while(scanf("%d",&n)!=EOF){
printf("%d\n",a[n]);
}
return 0;
}


poj1338
#include<stdio.h>

#define MIN(a,b) ((a>b)? (b):(a))

const int MAIX=1505;

int main()
{
int n;
int v1,v2,v3;
int p1,p2,p3;
int a[MAIX];
a[1]=1;
p1=p2=p3=1;
for(int i=2;i<MAIX;i++)
{
v1=a[p1]*2;
v2=a[p2]*3;
v3=a[p3]*5;
a[i]=MIN(MIN(v1,v2),v3);
if(v1==a[i]) p1++;
if(v2==a[i]) p2++;
if(v3==a[i]) p3++;
}
while(scanf("%d",&n),n)
printf("%d\n",a[n]);
return 0;
}

你可能感兴趣的:(多式子递推poj2545 poj2591 poj1338)