I really thought out ...... oh ...... Do not blame me
I think that has been the number of plug to the pile, know the number of the number of super multi n lot.
First of all, a lot more than this is difficult to control, secondly, the degree of complexity is not allowed.
So we consider a wave of dp:
f [i] denotes the i-th ugly number, then the number of ugly f [i] must be equal to f [k] * a [j] (where k <i).
So we have for each i, we enumerate k and j, is greater than f [i - 1] to find the smallest number that is f [i].
But this is the n- 2 * k, for the 1e5 ...... we have to consider optimization.
Then consider a wave:
Suppose f [i] = f [k] * a [j]
Suppose f [i + 1] = f [t] * a [j]
Then t must be greater than k, so for each j, k we have now is how much maintenance, start looking for each time from k + 1.
Such complexity becomes n * k + k * n (well understood), i.e. n * k.
And, long long ......
Code:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define maxn 400005 #define int long long #define inf 99999999999999 int p[maxn],f[maxn],s[maxn]; main() { int k,n; scanf("%lld%lld",&k,&n); for(int i=1;i<=k;i++) scanf("%lld",&p[i]); f[0]=1; for(int i=1;i<=n;i++) { int MIN=inf; for(int j=1;j<=k;j++) { while(f[i-1]>=f[s[j]]*p[j]) s[j]++; MIN=min(MIN,f[s[j]]*p[j]); } f[i]=MIN; } printf("%lld\n",f[n]); return 0; }