问题内容
Description
有一个数列,它是由自然数组成的,并且严格单调上升。最小的数不小于S,最大的不超过T。现在知道这个数列有一个性质:后一个数相对于前一个数的增长率总是百分比下的整数(如5相对于4的增长率是25%,25为整数;而9对7就不行了)。现在问:这个数列最长可以有多长?满足最长要求的数列有多少个?
Input Format
输入仅有一行,包含S和T两个数( 0<S<T≤200000 )。
30%的数据,0<S<T≤100 ;
100%的数据,0<S<T≤200000。
Output Format
输出有2行。第一行包含一个数表示长度,第二行包含一个数表示个数。
Sample Input
2 10
Sample Output
5
2
样例解释
2 4 5 6 9以及2 4 5 8 10
代码实现及分析
#include <stdio.h>
#include <memory.h>
#define MAXLEN 200001
long long lens[MAXLEN] = {0};
long long cnts[MAXLEN] = {0};
int main() {
long long s, t, max = 0;
long long maxcnt,i, j, proint;
double product;
while(EOF != scanf("%lld %lld",&s,&t)){
for(i = s; i <= t; i++)
{
lens[i] = 1;
cnts[i] = 1;
}
for (i = t,max = 1,maxcnt = 0; i > s; i--)
{
for (j = 1; j <= 100; j++) //遍历101%~200%,200%以上留给下一个数去找
{
product = i * 100 / (100.0 + j);
proint = i * 100 / (100.0 + j);
if(proint < s){
break;
}
if (product == proint)
{
if(lens[proint] < lens[i] + 1){
lens[proint] = 1 + lens[i];
cnts[proint] = cnts[i];
}
else{
if(lens[proint] == lens[i] + 1){
cnts[proint] += cnts[i];
}
}
if(max < lens[proint]){
max = lens[proint];
maxcnt = 0;
}
}
}
}
for(i = s,maxcnt = 0;i<=t;i++){
if(lens[i]==max){
maxcnt+=cnts[i];
}
}
printf("%lld\n", max);
printf("%lld\n", maxcnt);
}
return 0;
}
时间:393ms 空间:16096kb