File tree 1 file changed +51
-0
lines changed
1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+ using ll = int64_t ;
5
+ using ff = long double ;
6
+
7
+ vector<int > digits (ll x) {
8
+ vector<int > xs;
9
+ while (x > 0 ) {
10
+ xs.push_back (x % 10 );
11
+ x /= 10 ;
12
+ }
13
+ return xs;
14
+ }
15
+
16
+ ll g1 (vector<int >& digits) {
17
+ sort (digits.begin (), digits.end (), greater<int >());
18
+ ll y = 0 ;
19
+ for (auto d : digits) {
20
+ y = y * 10 + d;
21
+ }
22
+ return y;
23
+ }
24
+ ll g2 (vector<int >& digits) {
25
+ sort (digits.begin (), digits.end (), less<int >());
26
+ ll y = 0 ;
27
+ for (auto d : digits) {
28
+ y = y * 10 + d;
29
+ }
30
+ return y;
31
+ }
32
+ ll f (ll x) {
33
+ vector<int > xs = digits (x);
34
+ return g1 (xs) - g2 (xs);
35
+ }
36
+
37
+ int main () {
38
+ ios_base::sync_with_stdio (false );
39
+ cin.tie (0 ); cout.tie (0 );
40
+
41
+ int N, K;
42
+ cin >> N >> K;
43
+
44
+ ll a = N;
45
+ for (int i = 0 ; i < K; ++i) {
46
+ a = f (a);
47
+ }
48
+ cout << a << endl;
49
+
50
+ return 0 ;
51
+ }
You can’t perform that action at this time.
0 commit comments