1138. Integer Percentage
Time Limit: 1.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
Applying for a new job, programmer N. Smart required that his new salary (in rubles, positive integer) would be greater than his previous salary by integer percentage. What could be the highest possible number of previous jobs for mister Smart, if his latest salary did not exceed n rubles and his first salary was exactly s rubles?
Example. Let
n = 10,
s = 2, then
m = 5. The sequence 2, 4 (+100%), 5 (+25%), 8 (+60%), 10 (+25%) is the longest (although not unique) sequence that satisfies to the problem statement. Salary increase percentage is written inside the brackets.
Input
Two integers
n and
s separated by one or more spaces. 1 ≤
n,
s ≤ 10000.
Output
A single integer
m — the maximum number of N. Smart’s previous jobs.
Sample
input | output |
---|---|
10 2 | 5 |
Hint
if
n =
s, the answer is 1.
简单得很的一道动规题,但是没有看懂题意。一是最终工资不用一定等于n,可以小于等于n。二是,输出最多的工作数,而不是在满足工资最接近n时最多的工资数。
#include <cstdio>
#include <cmath>
#define MAX(a,b) ((a)>(b)?(a):(b))
long f[3000000];
int main()
{
freopen("ip.in","r",stdin);
freopen("ip.out","w",stdout);
long n, s;
if (n == s)
{
printf("0");
return 0;
}
scanf("%ld%ld",&n,&s);
f[s] = 1;
long ans = 1;
for (long i=s;i<n+1;i++)
{
for (long j=1;j<101 && i*100+i*j<n*100+1;j++)
{
if (!((i*j) % 100))
{
if (f[i] && f[i]+1 > f[i+i*j/100])
{
f[i+(i*j)/100] = f[i]+1;
ans = MAX(ans,f[i+(i*j)/100]);
}
}
}
}
printf("%ld",ans);
return 0;
}