0%

[題解]醜數

a075. 醜數

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define double long double
#define N 14
#define Orz ios::sync_with_stdio(0),cin.tie(0)
#define INF 2e18
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define rrep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
using namespace std;
int n;

signed main(){
Orz;
cin>>n;
vector<int> vec;
vec.push_back(1);
int id2 = 0,id3 = 0,id5 = 0;
//用前面的醜數推後面的,不會有其他質樹
for(int i=1;i<n;i++){
int ugly = min(vec[id2]*2,min(vec[id3]*3,vec[id5]*5));
vec.push_back(ugly);
if(vec[i]==vec[id2]*2)id2+=1;
if(vec[i]==vec[id3]*3)id3+=1;
if(vec[i]==vec[id5]*5)id5+=1;
}
cout<<vec[n-1]<<endl;
}