text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
vector<long long> al[100001];
long long vis[100001];
long long no(long long num) {
long long min = num % 10, max = num % 10;
while (num > 0) {
long long a = num % 10;
num /= 10;
if (min > a) min = a;
if (max < a) max = a;
}
return min * max;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
for (long long i = 0; i < k - 1; i++) {
long long b = no(a);
if (b == 0) break;
a += b;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int MIN(long long int n) {
long long int ans = 1000000000000000000;
while (n != 0) {
ans = min(ans, n % 10);
n /= 10;
}
return ans;
}
long long int MAX(long long int n) {
long long int ans = 0;
while (n != 0) {
ans = max(ans, n % 10);
n /= 10;
}
return ans;
}
int main() {
int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
for (long long int i = 2; i <= k; i++) {
if (MIN(a) == 0) break;
a = a + MIN(a) * MAX(a);
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void calc(long long n, long long &mx, long long &mn) {
while (n) {
long long rem = n % 10;
n /= 10;
mn = min(mn, rem);
mx = max(mx, rem);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long i, tc, cs = 1, n, m, k, cnt = 0, j, q, mn, mx;
string s;
cin >> tc;
while (tc--) {
cin >> n >> k;
mn = 9LL;
mx = 0LL;
while (--k) {
mn = 9LL;
mx = 0LL;
calc(n, mx, mn);
n += mx * mn;
if (mn == 0) break;
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
k--;
string s;
while (k--) {
s = to_string(a);
sort(s.begin(), s.end());
a += (s[0] - '0') * (s[s.size() - 1] - '0');
if (s[0] == '0') break;
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<bool> prime(200002, true);
vector<int> primes;
void SieveOfEratosthenes(long long n) {
prime[0] = prime[1] = false;
for (long long p = 2; p * p <= n; p++) {
if (prime[p]) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (int i = 0; i < n; i++) {
if (prime[i]) primes.push_back(i);
}
}
int min_ret(vector<int> &a) {
int min = a[0];
for (int i = 1; i < a.size(); i++) {
if (min > a[i]) min = a[i];
}
return min;
}
int max_ret(vector<int> &a) {
int max = a[0];
for (int i = 1; i < a.size(); i++) {
if (max < a[i]) max = a[i];
}
return max;
}
int main() {
int t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
for (int i = 0; i < k - 1; i++) {
long long y = a;
vector<int> num;
while (y) {
num.push_back(y % 10);
y /= 10;
}
int z = num.size();
if (min_ret(num) == 0)
break;
else {
a += min_ret(num) * max_ret(num);
}
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000007;
void min_max(int a, int& cur_min, int& cur_max, bool three) {
if (three && a < 100) cur_min = 0;
while (a > 0) {
auto tmp = a % 10;
cur_min = min(cur_min, tmp);
cur_max = max(cur_max, tmp);
a /= 10;
}
}
int main() {
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
string A;
long long int k;
cin >> A >> k;
int min_left = 9, max_left = 0;
for (int i = 0; i < int(A.size()) - 3; i++) {
min_left = min(min_left, A[i] - '0');
max_left = max(max_left, A[i] - '0');
}
int cur_min, cur_max;
int a = 0;
for (int i = A.size() - 1; i >= 0 && i >= int(A.size()) - 3; i--) {
a += (A[i] - '0') * pow(10, A.size() - 1 - i);
}
bool three = A.size() >= 3;
A.resize(max(0, int(A.size()) - 3));
for (int i = 1; i < k; i++) {
cur_min = min_left, cur_max = max_left;
min_max(a, cur_min, cur_max, three);
if (cur_min == 0) break;
a += cur_min * cur_max;
}
int resid = a / 1000;
for (int i = int(A.size()) - 1; i >= 0; i--) {
int tmp = A[i] - '0' + resid;
resid = tmp / 10;
A[i] = (tmp % 10) + '0';
}
if (resid) cout << resid;
cout << A;
a = a % 1000;
if (three)
cout << a / 100 << (a % 100) / 10 << a % 10 << endl;
else
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long multiply(unsigned long long n) {
unsigned long long minm = 9, maxm = 0;
while (n > 0) {
minm = min(minm, n % 10);
maxm = max(maxm, n % 10);
n /= 10;
}
return minm * maxm;
}
int main() {
int t;
cin >> t;
while (t--) {
unsigned long long a, n;
cin >> a >> n;
for (int i = 2; i <= n; i++) {
unsigned long long addMinMaxDigits = multiply(a);
if (addMinMaxDigits == 0) {
break;
} else
a += addMinMaxDigits;
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
void solve() {
int Q;
cin >> Q;
while (Q--) {
ll A, K;
cin >> A >> K;
--K;
while (K > 0) {
ll mx = 0;
ll mn = 9;
ll At = A;
while (At > 0) {
ll r = At % 10;
At /= 10;
mx = max(mx, r);
mn = min(mn, r);
}
if (mx * mn == 0) {
break;
}
A += mx * mn;
--K;
}
cout << A << "\n";
}
}
int main() {
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
solve();
cout << flush;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int getAdd(long long int x) {
int m1 = 10, m2 = 0;
while (x > 0) {
int y = x % 10;
x = x / 10;
m1 = min(m1, y);
m2 = max(m2, y);
}
return m1 * m2;
}
int main() {
int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
k--;
while (k--) {
long long int y = getAdd(n);
if (y == 0) break;
n = n + y;
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
void solve() {
long long a;
long long k;
cin >> a >> k;
k--;
while (k && to_string(a).find("0") == string::npos) {
k--;
string s = to_string(a);
int _min = (*min_element(s.begin(), s.end())) - '0';
int _max = (*max_element(s.begin(), s.end())) - '0';
a += _min * _max;
}
cout << a << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
void debug_v(const vector<int> &data) {
cout << "data: [";
for (int i = 0; i < data.size(); ++i) {
cout << data[i] << ", "[i == data.size() - 1];
}
cout << "]" << '\n';
}
long long INF = 1e18;
long long min_(long long num) {
if (num == 0) return num;
long long ans = INF;
while (num > 0) {
ans = min(ans, num % 10);
num /= 10;
}
return ans;
}
long long max_(long long num) {
if (num == 0) return num;
long long ans = 0;
while (num > 0) {
ans = max(ans, num % 10);
num /= 10;
}
return ans;
}
void solve() {
long long a, k;
cin >> a >> k;
long long count = 1;
unordered_map<long long, bool> mp;
vector<long long> cache;
long long tmp = 0;
while (count != k) {
if (mp[a]) {
cout << cache[k % cache.size()] << '\n';
return;
}
mp[a] = true;
cache.push_back(a);
tmp = max_(a) * min_(a);
a = a + tmp;
if (tmp == 0) {
cout << a << '\n';
return;
}
count++;
}
cout << a << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return EXIT_SUCCESS;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, k, dig, mm, ma;
cin >> n >> k;
long long int ans = 0;
for (long long int i = 1; i < k; i++) {
long long int temp = n + ans;
mm = 9, ma = 0;
while (temp > 0) {
dig = temp % 10;
mm = min(mm, dig);
ma = max(ma, dig);
temp /= 10;
}
if ((mm * ma) == 0)
break;
else
ans += mm * ma;
}
cout << (n + ans) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mind(long long int a) {
long long int m = 9;
while (a) {
long long int x = a % 10;
m = min(m, x);
a /= 10;
}
return m;
}
long long int maxd(long long int a) {
long long int m = -1;
while (a) {
long long int x = a % 10;
m = max(m, x);
a /= 10;
}
return m;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
long long int i = 1;
while (i < k and mind(a) != 0) {
a = a + maxd(a) * mind(a);
i++;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAXN = 200005;
long long val(long long x) {
long long m = 9, M = 0;
while (x) {
m = min(m, x % 10);
M = max(M, x % 10);
x /= 10;
}
return m * M;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int __tc;
cin >> __tc;
for (long long case_num = (1); (case_num) < (__tc + 1); ++case_num) {
long long x, k;
cin >> x >> k;
if (k == 1) {
cout << x << "\n";
} else {
k--;
while (k--) {
long long v = val(x);
if (!v) {
break;
}
x += v;
}
cout << x << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, k;
cin >> n >> k;
long long int num = n;
for (long long int i = 2; i <= k; ++i) {
long long int mia = 10, maa = -1;
long long int temp = num;
while (temp) {
mia = min(mia, temp % 10);
maa = max(maa, temp % 10);
temp = temp / 10;
}
if (mia == 0) {
break;
}
num = num + mia * maa;
}
cout << num << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
for (int i = 2; i <= k; i++) {
long long int temp = a, x = 0, y = 10;
while (temp) {
y = min((temp % 10), y);
x = max((temp % 10), x);
temp = temp / 10;
}
if (y == 0) break;
a += (x * y);
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll p(ll a) {
ll x = 10;
ll y = -1;
while (a > 0) {
x = min(a % 10, x);
y = max(a % 10, y);
a /= 10;
}
return x * y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
ll a, k;
cin >> a >> k;
k--;
while (k--) {
a += p(a);
if (p(a) == 0) break;
}
cout << a << '\n';
}
}
|
#include <bits/stdc++.h>
int product(long long a) {
int maxi = 0, mini = 9, x, ans;
long long temp = a;
while (temp) {
x = temp % 10;
if (x < mini) mini = x;
if (x > maxi) maxi = x;
temp /= 10;
}
if (mini == 0) return 0;
ans = mini * maxi;
return ans;
}
using namespace std;
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
long long a, k, ans;
int t;
cin >> t;
while (t--) {
cin >> a >> k;
k--;
while (k--) {
if (product(a) == 0)
break;
else {
a += product(a);
}
}
cout << a << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> return_digit(long long num) {
vector<long long> arr;
while (num != 0) {
long long rem = num % 10;
arr.push_back(rem);
num /= 10;
}
return arr;
}
long long product_minmax(vector<long long> v) {
long long max_val = v[0];
long long min_val = v[0];
for (long long i = 1; i < v.size(); i++) {
max_val = max(max_val, v[i]);
min_val = min(min_val, v[i]);
}
return max_val * min_val;
}
long long compute(long long num) {
return num + product_minmax(return_digit(num));
}
int main() {
long long t;
cin >> t;
while (t--) {
long long a1, k;
cin >> a1 >> k;
long long temp = a1;
long long i = 0;
long long prev = a1;
while (i < k - 1) {
prev = temp;
temp = compute(temp);
if (prev == temp) {
break;
}
i += 1;
}
cout << temp << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> cal(long long a) {
vector<long long> v(2);
v[0] = 9;
v[1] = 0;
while (a) {
int t = a % 10;
a /= 10;
if (t < v[0]) v[0] = t;
if (t > v[1]) v[1] = t;
}
return v;
}
int main() {
int t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
long long sum = a;
vector<long long> v(2);
v = cal(a);
k--;
while (k-- && v[0] != 0) {
sum += v[0] * v[1];
v = cal(sum);
}
cout << sum << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long double PI = acosl(-1);
const long long infl = 1e17 + 100;
const int inf = 1e9 + 100;
const int nmax = 1e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tc;
cin >> tc;
for (int ces = 1; ces <= tc; ces++) {
long long a, k;
cin >> a >> k;
k--;
while (k--) {
vector<int> digs;
long long x = a;
while (x) digs.push_back(x % 10), x /= 10;
sort(digs.begin(), digs.end());
if (digs[0] == 0) break;
a += digs[0] * digs.back();
}
cout << a << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int t;
int main() {
cin >> t;
while (t--) {
long long a;
long long k;
scanf("%lld %lld", &a, &k);
for (int i = 1; i < k; i++) {
long long aa = a;
long long minv = aa % 10, maxv = aa % 10;
while (aa) {
minv = min(minv, aa % 10);
maxv = max(maxv, aa % 10);
aa /= 10;
}
if (minv == 0) break;
a += minv * maxv;
}
printf("%lld\n", a);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
long long n, m;
cin >> n >> m;
for (int i = 1; i < m; i++) {
long long k = n, mi = 9, ma = 0;
while (k) {
mi = min(k % 10, mi);
ma = max(k % 10, ma);
k /= 10;
}
if (mi == 0)
break;
else
n += mi * ma;
}
cout << n << endl;
}
}
|
#include <bits/stdc++.h>
int solve_testcase() {
intmax_t a, k;
scanf("%jd %jd", &a, &k);
--k;
for (intmax_t i = 0; i < k; ++i) {
std::string s = std::to_string(a);
auto [it0, it1] = std::minmax_element(s.begin(), s.end());
int mi = *it0 - '0';
int ma = *it1 - '0';
if (mi * ma == 0) break;
a += mi * ma;
}
printf("%jd\n", a);
return 0;
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve_testcase();
}
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 4;
int nooffac(long long a) {
int c = 0;
for (int i = 1; i * i <= a; i++) {
if (a % i == 0) {
c++;
if (a / i != i) c++;
}
return c;
}
}
long long check(long long val) {
long long k2 = val;
long long t = 0;
long long sum = 0;
val = val - val % 5;
while (k2 > 0) {
k2 = k2 / 5;
t++;
}
t--;
long long k1 = pow(5, t);
while (k1 >= 5) {
sum += val / k1;
k1 = k1 / 5;
}
return sum;
}
long long bi_search(long long k) {
long long low = 0;
long long high = 1e12;
long long pos;
while (high > low) {
long long mid = (low + high) / 2;
if (check(mid) >= k) {
pos = mid;
high = mid - 1;
} else
low = mid + 1;
}
return pos;
}
int main() {
long long n;
scanf("%lld", &n);
for (int i = 0; i < n; i++) {
long long a, b;
scanf("%lld", &a);
scanf("%lld", &b);
int i1 = 2;
while (i1 <= b) {
long long k = a;
long long m1 = INT_MAX;
long long m2 = INT_MIN;
while (k) {
m1 = min(k % 10, m1);
m2 = max(k % 10, m2);
k /= 10;
}
a += m1 * m2;
if (m1 == 0) break;
i1++;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long nxt(long long x) {
long long mn = 10;
long long mx = 0;
long long xx = x;
while (x > 0) {
mn = min(mn, x % 10);
mx = max(mx, x % 10);
x /= 10;
}
return xx + mn * mx;
}
signed main() {
long long t;
cin >> t;
while (t--) {
long long a1, k;
cin >> a1 >> k;
k--;
while (k--) {
if (a1 == nxt(a1)) break;
a1 = nxt(a1);
}
cout << a1 << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long long a, k;
cin >> a >> k;
long long max;
long long min;
int temp = 0;
while (k--) {
min = 9;
max = 0;
long long h = a;
while (h > 0) {
int f = h % 10;
h = h / 10;
if (f < min) {
min = f;
}
if (f > max) {
max = f;
}
if (min == 0) {
cout << a << endl;
temp = 1;
break;
}
}
if (temp == 1) {
break;
}
a = a + max * min;
}
if (temp != 1) cout << a - max * min << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int minDigit(long long a) {
if (a == 0) return a;
long long answer = 9;
while (a > 0) {
answer = min(answer, a % 10);
a /= 10;
}
return answer;
}
int maxDigit(long long a) {
long long answer = 0;
while (a > 0) {
answer = max(answer, a % 10);
a /= 10;
}
return answer;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
long long prev = -1;
while (a != prev && k > 1) {
prev = a;
a = a + minDigit(a) * maxDigit(a);
k--;
}
cout << a << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
long long product(long long n) {
int x, mini = 20, maxi = -1;
while (n != 0) {
x = n % 10;
if (x < mini) mini = x;
if (x > maxi) maxi = x;
n = n / 10;
}
return maxi * mini;
}
long long swd(long long n, long long k) {
if (k == 1)
return n;
else {
int x = product(n);
if (x == 0)
return n;
else
return swd(n + x, k - 1);
}
}
void solve() {
long long n, k, ans;
cin >> n >> k;
ans = swd(n, k);
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
unsigned long long mind(unsigned long long n) {
if (n < 10) return n;
return min(n % 10, mind(n / 10));
}
unsigned long long maxd(unsigned long long n) {
if (n < 10) return n;
return max(n % 10, maxd(n / 10));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
unsigned long long n, k;
scanf("%llu %llu", &n, &k);
while (k > 1 && n > 0) {
if (mind(n) == 0) break;
n = n + mind(n) * maxd(n);
k--;
}
printf("%llu\n", n);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int a1;
cin >> a1;
long long int k;
cin >> k;
while (k > 1) {
long long int temp = a1;
long long int mind, maxd;
mind = INT_MAX;
maxd = INT_MIN;
while (temp > 0) {
int n = temp % 10;
if (n > maxd) {
maxd = n;
}
if (n < mind) {
mind = n;
}
temp = temp / 10;
}
a1 = a1 + mind * maxd;
k--;
if (mind == 0) {
break;
}
}
cout << a1 << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const long long N = 2e5 + 7;
const long long M = 1e9 + 7;
const long long MAXN = 1e18 + 7;
const long long Mod = 998244353;
long long _, i, j, k, n, m, p, q, s, t, T, d, l, r, h, len, o, u, v, w, st, en,
sx, sy, ma, mi, mod, sum, num, res, flag, cas, ret, mid, ans, cnt, now, nex,
tmp;
long long a[N], c[N];
char ch;
vector<long long> g[N];
string s1, s2, s3;
signed main() {
ios::sync_with_stdio(false);
long long T = 1;
cin >> T;
while (T--) {
cin >> n >> k;
tmp = n;
for (i = 1; i < k; i++) {
ma = 0;
mi = 9;
n = tmp;
while (n != 0) {
m = n % 10;
ma = max(ma, m);
mi = min(mi, m);
n = n / 10;
}
if (mi == 0) break;
tmp = tmp + ma * mi;
}
cout << tmp << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mk = 100055;
const long long logn = 29;
const long long mod = 1e9 + 7;
long long n, m, q, min_dig = 9, max_dig = 0, a1, k, t;
vector<int> g[mk];
bool check(long long a) {
long long num = a;
min_dig = 9ll, max_dig = 0ll;
while (num) {
if (num % 10 == 0) return false;
min_dig = min(min_dig, num % 10);
max_dig = max(max_dig, num % 10);
num /= 10;
}
return true;
}
void init() {
cin >> a1 >> k;
k--;
while (check(a1) && k) {
a1 = a1 + min_dig * max_dig;
k--;
}
cout << a1 << "\n";
}
signed main() {
cin >> t;
while (t--) init();
}
|
#include <bits/stdc++.h>
using namespace std;
int t;
long long i, K, a[1000005];
int Maxdigit(long long x) {
int Max = x % 10;
x /= 10;
for (; x; x /= 10) Max = max(Max * 1LL, x % 10);
return Max;
}
int Mindigit(long long x) {
int Min = x % 10;
x /= 10;
for (; x; x /= 10) Min = min(Min * 1LL, x % 10);
return Min;
}
int main(void) {
scanf("%d", &t);
for (; t; t--) {
scanf("%lld%lld", &a[1], &K);
for (i = 2; i <= K; i++) {
a[i] = a[i - 1] + Maxdigit(a[i - 1]) * Mindigit(a[i - 1]);
if (a[i] == a[i - 1]) break;
}
if (i <= K)
printf("%lld\n", a[i]);
else
printf("%lld\n", a[K]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(false);
int tc;
cin >> tc;
while (tc--) {
long long a, k;
cin >> a >> k;
while (k > 1) {
int minx = 9;
int mx = 0;
long long num = a;
while (num > 0) {
int rem = num % 10;
minx = min(minx, rem);
mx = max(mx, rem);
num /= 10;
}
if (minx == 0) {
break;
}
a = a + minx * mx;
k--;
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
x = 0;
int ch = getchar(), f = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') f = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
if (f) x = -x;
}
const int inf = 0x3f3f3f3f;
int T;
long long a, k;
int main() {
cin >> T;
while (T--) {
cin >> a >> k;
k--;
long long minx, maxx;
minx = inf, maxx = -inf;
long long cnt = 0;
while (minx != 0 && cnt < k) {
long long temp = a;
minx = inf, maxx = -inf;
while (temp) {
minx = min(minx, temp % 10);
maxx = max(maxx, temp % 10);
temp /= 10;
}
a += minx * maxx;
cnt++;
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long GcdRecursive(long long a, long long b) {
if (b == 0) return a;
return GcdRecursive(b, a % b);
}
const long long mod = 1e9 + 7;
int dirX[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dirY[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const long long MOD = 998244353;
const long long N = 2e5 + 7;
const long long LLinf = 1e18 + 5;
const int INTinf = 1e8 + 7;
long long t, n, m, a, b, k, c = 0, d, x, y;
long long x11, x22, y11, y22;
long long get(long long a) {
long long mn = 9, mx = 0;
while (a > 0) {
long long x = a % 10;
a /= 10;
mn = min(x, mn);
mx = max(x, mx);
}
return mn * mx;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> t;
while (t--) {
cin >> a >> k;
k--;
while (k--) {
long long ans = get(a);
if (ans == 0) break;
a += ans;
}
cout << a << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
for (int i = 1; i <= k - 1; i++) {
string str = to_string(a);
sort(str.begin(), str.end());
long long first = str[0] - '0';
long long second = str[str.size() - 1] - '0';
a += first * second;
if (first == 0 || second == 0) break;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
set<int> si;
mt19937_64 rang(
chrono::high_resolution_clock::now().time_since_epoch().count());
unsigned long long hu(unsigned long long k) {
vector<unsigned long long> v;
for (unsigned long long i = 1; i < 100000000;) {
if (k == 0) {
break;
}
v.push_back(k % 10);
k = k / 10;
}
sort(v.begin(), v.end());
return v.front() * v.back();
}
void mi() {
unsigned long long k, n;
cin >> k >> n;
for (unsigned long long i = 1; i < n; i++) {
k = k + hu(k);
if (hu(k) == 0) {
break;
}
}
cout << k << "\n";
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
unsigned long long t;
cin >> t;
while (t--) {
mi();
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mindig(long long int n);
long long int maxdig(long long int n);
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, k, i, j;
cin >> n >> k;
k--;
while (k != 0) {
i = mindig(n);
j = maxdig(n);
n += (i * j);
if (i * j == 0) break;
k--;
}
cout << n << endl;
}
}
long long int mindig(long long int n) {
long long int ans = 10;
while (n >= 1) {
ans = min(ans, n % 10);
n = n / 10;
}
return ans;
}
long long int maxdig(long long int n) {
long long int ans = 0;
while (n >= 1) {
ans = max(ans, n % 10);
n = n / 10;
}
return ans;
}
|
#include <bits/stdc++.h>
using namespace std;
long long prime(long long n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return 0;
return 1;
}
int main() {
long long t;
cin >> t;
long long n, k, i, j, min, max, temp;
while (t--) {
cin >> n >> k;
for (i = 1; i < k; i++) {
temp = n;
min = INT_MAX;
max = INT_MIN;
while (temp > 0) {
j = temp % 10;
if (j > max) max = j;
if (j < min) min = j;
temp = temp / 10;
}
if (min == 0) break;
n = n + max * min;
}
cout << n << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, k, mini, maxi, temp;
cin >> n >> k;
for (int i = 1; i <= k - 1; i++) {
mini = 9, maxi = -1;
temp = n;
while (temp > 0) {
mini = min(mini, temp % 10);
maxi = max(maxi, temp % 10);
temp /= 10;
}
if (mini == 0) break;
n += (mini * maxi);
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long digit(long long a) {
long long Min = 10, Max = -1;
while (a > 0) {
Min = min(Min, a % 10);
Max = max(Max, a % 10);
a /= 10;
}
return Min * Max;
}
void solve() {
long long a1, K;
cin >> a1 >> K;
long long a = a1;
for (long long i = 2; i <= K; i++) {
long long d = digit(a);
if (d == 0) {
break;
}
a += d;
}
cout << a << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 228;
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
long long q;
cin >> q;
while (q--) {
long long n, k;
cin >> n >> k;
--k;
while (k--) {
long long mn = 9, mx = 0;
long long j = n;
while (j) mn = min(mn, j % 10), mx = max(mx, j % 10), j /= 10;
if (mn == 0) break;
n += mn * mx;
}
cout << n << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long mod_inv(long long a, long long m) {
a = a % m;
return power(a, m - 2, m) % 10 ^ 9 + 7;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
bool mod(double a, double b) { return a / b - floor(a / b); }
long long logb(long long base, long long x) { return (log(x) / log(base)); }
long long parent[1000000], Size[10000000];
long long find_set(long long x) {
if (x == parent[x]) return x;
return parent[x] = find_set(parent[x]);
}
void Union(long long x, long long y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
if (Size[x] > Size[y]) {
parent[y] = parent[x];
Size[x] += Size[y];
} else {
parent[x] = parent[y];
Size[y] += Size[x];
}
}
}
void make_set(long long x) {
parent[x] = x;
Size[x] = 1;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
k--;
while (k) {
k--;
long long temp = a, mn = 10, ma = -1, rem;
while (temp) {
rem = temp % 10;
temp /= 10;
mn = min(mn, rem);
ma = max(ma, rem);
}
a += (mn * ma);
if (mn == 0) break;
}
cout << a << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline void solve() {
long long a, k;
cin >> a >> k;
long long ans = 0;
for (long long i = 0; i < k - 1; ++i) {
long long temp = a;
vector<long long> digit;
while (temp > 0) {
digit.push_back(temp % 10);
temp /= 10;
}
sort(digit.begin(), digit.begin() + digit.size());
if (digit[0] == 0 || digit[digit.size() - 1] == 0) {
cout << a << "\n";
return;
}
a = a + digit[0] * digit[digit.size() - 1];
}
cout << a << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long q = 1;
cin >> q;
while (q--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-7;
const long long MOD = 1000000007LL;
const int INF = 0x3f3f3f3f;
const int NIL = -1;
template <typename T>
void read(T &x) {
x = 0;
char ch = getchar();
long long f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
const int maxn = 1e5 + 10;
int solve(long long num) {
int maxx = -1, minn = INF;
while (num) {
int t = num % 10;
maxx = max(maxx, t);
minn = min(minn, t);
num /= 10;
}
return maxx * minn;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long a, b;
scanf("%lld%lld", &a, &b);
long long sum = a;
for (int i = 2; (long long)i <= b; ++i) {
long long num = solve(sum);
sum += num;
if (num == 0) break;
}
printf("%lld\n", sum);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a) out << it << " ";
return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a) in >> i;
return in;
}
ifstream cinn("input.txt");
ofstream coutt("output.txt");
long long f(long long n) {
if (n == 0) return 0;
long long mn = (1e18), mx = -(1e18);
while (n) {
mn = min(mn, n % 10);
mx = max(mx, n % 10);
n /= 10;
}
return mn * mx;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long a1, k;
cin >> a1 >> k;
long long lst = a1;
long long ans = a1;
for (long long i = 2; i <= k; i++) {
ans += f(lst);
lst = ans;
if (f(lst) == 0) {
break;
}
}
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;
int dif(ll x) {
int mn = 10, mx = 0;
while (x) {
mn = min(mn, int(x % 10));
mx = max(mx, int(x % 10));
x /= 10;
}
return mn * mx;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
ll a1, k;
scanf("%lld %lld", &a1, &k);
while (--k) {
int d = dif(a1);
if (!d) break;
a1 += d;
}
printf("%lld\n", a1);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long num, i, j, k, l, mn1, mx1;
cin >> num >> k;
for (long long i = 1; i < k; i++) {
j = num;
mn1 = 9;
mx1 = 0;
while (j > 0) {
l = j % 10;
mn1 = min(l, mn1);
mx1 = max(l, mx1);
j = j / 10;
}
if (mn1 == 0) {
break;
}
num = num + mn1 * mx1;
}
cout << num << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n, k;
scanf("%lld%lld", &n, &k);
for (int i = 0; i < k - 1; i++) {
string a;
stringstream ss;
ss << n;
ss >> a;
char mini = *min_element(a.begin(), a.end()),
maxi = *max_element(a.begin(), a.end());
if (mini == '0')
break;
else
n += ((mini - '0') * (maxi - '0'));
}
printf("%lld", n);
printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int arr(long long a) {
long long n = a;
int max = -1;
int min = 10;
while (n > 0) {
if (n % 10 > max) {
max = n % 10;
}
if (n % 10 < min) {
min = n % 10;
}
n = n / 10;
}
return max * min;
}
int main() {
int t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
long long first = n, end = n;
for (long long i = 1; i < m; i++) {
first = end + arr(end);
if (arr(end) == 0) {
break;
}
end = first;
}
cout << first << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long t, n, m, k;
signed main() {
cin >> t;
while (t--) {
cin >> n >> m;
m--;
while (m--) {
long long q = n;
long long mn = 10, mx = -1;
while (q) {
long long y = q % 10;
q /= 10;
if (y < mn) mn = y;
if (y > mx) mx = y;
}
n += mn * mx;
if (mn == 0 || mx == 0) break;
}
cout << n << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int mindigit(long long int u) {
long long int min = 9;
while (u != 0) {
if (min >= (u % 10)) {
min = u % 10;
}
u = u / 10;
}
return min;
}
long long int maxdigit(long long int u) {
long long int max = 0;
while (u != 0) {
if (max <= (u % 10)) {
max = u % 10;
}
u = u / 10;
}
return max;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
long long int t, a, k;
cin >> t;
while (t--) {
cin >> a >> k;
long long int num = a;
long long int som = 1;
while ((mindigit(num) != 0) and (som < k)) {
num = num + mindigit(num) * maxdigit(num);
som++;
}
cout << num << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
long long n, m;
cin >> n >> m;
m--;
while (m--) {
long long x = n;
long long a = 11, b = -1;
while (x) {
a = min(a, x % 10);
b = max(b, x % 10);
x /= 10;
}
if (!a || !b)
break;
else
n += (a * b);
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long a, k, b = 0;
cin >> a >> k;
while (--k && b != a) {
b = a;
int ma = 0, mi = 10;
while (a) {
int p = a % 10;
ma = max(ma, p);
mi = min(mi, p);
a /= 10;
}
a = b + ma * mi;
}
cout << a << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, k;
cin >> n >> k;
k--;
while (k--) {
long long N = n;
long long mx = -1, mi = 10;
while (N > 0) {
mx = max(mx, N % 10);
mi = min(mi, N % 10);
N /= 10;
}
n += (mi * mx);
if (mi == 0) break;
}
cout << n << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
string a;
long long k, l, s;
char ma, mi;
cin >> a >> k;
if (k == 1) {
cout << a << endl;
} else {
for (int i = 0; i < k - 1; i++) {
l = a.size();
ma = a[0];
mi = a[0];
for (int j = 1; j < l; j++) {
if (a[j] > ma) {
ma = a[j];
}
}
for (int j = 1; j < l; j++) {
if (a[j] < mi) {
mi = a[j];
}
}
int ma2 = ma - '0';
int mi2 = mi - '0';
if (mi2 == 0) {
break;
} else {
stringstream ssr(a);
ssr >> s;
s += ma2 * mi2;
stringstream sr;
sr << s;
sr >> a;
}
}
cout << a << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int pro(long long int l) {
long long int maxi = 0, mini = 10;
while (l > 0) {
long long int y = l % 10;
l = l / 10;
maxi = max(maxi, y);
mini = min(mini, y);
}
return maxi * mini;
}
int main() {
long int T;
cin >> T;
while (T--) {
long long int k, l;
cin >> l >> k;
for (long long int i = 0; i < k - 1; i++) {
long long int sum = pro(l);
if (sum == 0) {
break;
}
l = l + sum;
}
cout << l << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
long long int ans = n;
long long int i = 1;
while (i < k) {
long long int p = n;
long long int minn = 10, maxx = 0;
while (p) {
long long int pp = p % 10;
minn = min(minn, pp);
maxx = max(maxx, pp);
p = p / 10;
}
long long int kp = minn * maxx;
n = n + kp;
if (minn == 0) break;
i++;
}
cout << n << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long findMaxMin(long long a) {
long long mx = 0, mn = 10;
while (a) {
mx = max(mx, a % 10);
mn = min(mn, a % 10);
a /= 10;
}
return mx * mn;
}
void solve() {
long long a, k;
while (cin >> a >> k) {
long long sum = a;
for (long long i = 1; i <= k - 1; i++) {
long long add = findMaxMin(a);
sum += add;
if (add == 0) break;
a = sum;
}
cout << sum << endl;
}
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc;
cin >> tc;
while (tc--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int a, k, b, m = 10, m1 = 0;
cin >> a >> k;
b = a;
while (k > 1) {
m = 10, m1 = 0;
while (b > 0) {
long long int i = b % 10;
m = min(m, i);
m1 = max(m1, i);
b /= 10;
}
if (m == 0) break;
a += m * m1;
b = a;
k--;
}
cout << a << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t = 0;
cin >> t;
while (t--) {
long long a, K;
cin >> a >> K;
long long minD = 10, maxD = -1;
long long minD0 = 10, maxD0 = -1;
long long x = a;
x /= 1000;
while (x > 0) {
if (x % 10 > maxD0) maxD0 = x % 10;
if (x % 10 < minD0) minD0 = x % 10;
x /= 10;
}
while (--K) {
x = a % 1000;
minD = minD0, maxD = maxD0;
if (a > 999 && x < 100) minD = 0;
while (x > 0) {
if (x % 10 > maxD) maxD = x % 10;
if (x % 10 < minD) minD = x % 10;
x /= 10;
}
if (minD == 0) break;
a += minD * maxD;
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double EPS = 1e-9;
int INF = 1000000005;
long long INFF = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
long long int mm(long long int n) {
long long int ma = 0, mi = 10;
while (n > 0) {
long long int x = n % 10;
n /= 10;
mi = min(mi, x);
ma = max(ma, x);
}
return mi * ma;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
k--;
while (k--) {
long long int y = mm(a);
if (y == 0) break;
a += y;
}
cout << a << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << '\n';
err(++it, args...);
}
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int number_of_testcase;
cin >> number_of_testcase;
for (int testcase = 1; testcase <= number_of_testcase; ++testcase) {
long long n, k;
cin >> n >> k;
vector<int> arr;
string s = to_string(n);
for (char c : s) {
arr.push_back(c - '0');
}
bool found = false;
for (__typeof(k) i = (1) - (1 > k); i != (k) - (1 > k);
i += 1 - 2 * (1 > k)) {
n += *min_element(arr.begin(), arr.end()) *
*max_element(arr.begin(), arr.end());
if (*min_element(arr.begin(), arr.end()) *
*max_element(arr.begin(), arr.end()) ==
0) {
found = true;
cout << n << '\n';
break;
}
arr.clear();
s = to_string(n);
for (char c : s) {
arr.push_back(c - '0');
}
}
if (not found) cout << n << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &istream, vector<T> &v) {
for (auto &it : v) cin >> it;
return istream;
}
template <typename T>
ostream &operator<<(ostream &ostream, const vector<T> &c) {
for (auto &it : c) cout << it << " ";
return ostream;
}
long long int mult(long long int a, long long int b,
long long int p = 1000000007) {
return ((a % p) * (b % p)) % p;
}
long long int add(long long int a, long long int b,
long long int p = 1000000007) {
return (a % p + b % p) % p;
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
if (x < y) x = y;
}
long long int i, j;
void solve() {
long long int a, k;
cin >> a >> k;
long long int mini, maxi;
--k;
while (k--) {
string g = to_string(a);
mini = *min_element((g).begin(), (g).end()) - '0';
maxi = *max_element((g).begin(), (g).end()) - '0';
if (mini == 0) break;
a += mini * maxi;
}
cout << a << '\n';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int a1, k, a, sum = 0;
cin >> a1 >> k;
a = a1;
vector<int> v;
for (long long int i = 1; i < k; i++) {
long long int x = a;
while (a > 0) {
v.push_back(a % 10);
a /= 10;
}
int s = *min_element(v.begin(), v.end());
sum +=
*max_element(v.begin(), v.end()) * *min_element(v.begin(), v.end());
a = a1 + sum;
v.clear();
if (s == 0) {
break;
}
}
cout << a1 + sum << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 4 * N, D = 2e3 + 17, mod = 1e9 + 7, mod2 = 1e9 + 9;
const long long INF = 1e17;
int tt = 1;
long long n, k;
pair<int, int> f(long long n) {
long long mn = 9, mx = 0;
while (n) {
mx = max(mx, n % 10);
mn = min(mn, n % 10);
n /= 10;
}
return make_pair(mx, mn);
}
void solve() {
cin >> n >> k;
while (k && f(n).second) {
k--;
if (k == 0) {
break;
}
n = n + f(n).first * f(n).second;
}
cout << n << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long q, nr, k;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie();
}
int main() {
fast();
cin >> q;
while (q--) {
cin >> nr >> k;
k--;
while (k--) {
long long mn = 10, mx = -1, cnr = nr;
while (cnr) {
mn = min(mn, cnr % 10);
mx = max(mx, cnr % 10);
cnr /= 10;
}
if (!mn * mx)
break;
else
nr += mn * mx;
}
cout << nr << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
for (long long i = 0; i < k - 1; i++) {
set<long long> order;
long long next = n;
while (n) {
order.insert(n % 10);
n /= 10;
}
n = next + (*order.begin()) * (*order.rbegin());
if (*order.begin() == 0) break;
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mx = 1000000007;
void solve() {
long long n, k;
cin >> n >> k;
while (k > 1) {
long long x = n;
long long a = INT_MAX, b = INT_MIN;
while (x) {
a = min(a, x % 10);
b = max(b, x % 10);
x /= 10;
}
if (a * b == 0) break;
n += (a * b);
k--;
}
cout << n << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void getmx(long long n, vector<int> &V) {
V.clear();
while (n > 0) {
V.push_back(n % 10);
n /= 10;
}
}
long long getNK(long long n, long long k) {
vector<int> V;
int minv, maxv;
for (long long i = 1; i < k; i++) {
getmx(n, V);
minv = *min_element(V.begin(), V.end());
maxv = *max_element(V.begin(), V.end());
if (minv == 0)
break;
else
n += minv * maxv;
}
return n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
long long n, k;
cin >> T;
while (T--) {
cin >> n >> k;
cout << getNK(n, k) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
vector<long long int> v;
v.push_back(a);
for (int i = 1;; i++) {
long long int ma = 0, mi = 9, r = a, x;
while (r > 0) {
x = r % 10;
ma = max(ma, x);
mi = min(mi, x);
r = r / 10;
}
a += ma * mi;
v.push_back(a);
if (mi == 0) break;
}
if (k <= v.size())
cout << v[k - 1] << "\n";
else
cout << v[(v.size() - 1)] << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long Min(string s) {
long long min = s[0] - '0';
for (int i = 0; i < s.size(); i++) {
int c = s[i] - '0';
if (c < min) min = c;
}
return min;
}
long long Max(string s) {
long long max = 0;
for (int i = 0; i < s.size(); i++) {
int c = s[i] - '0';
if (c > max) max = c;
}
return max;
}
int main() {
string num;
long long n, t;
cin >> t;
for (int j = 0; j < t; j++) {
cin >> num >> n;
long long x = stoll(num);
for (int i = 1; i < n; i++) {
x += Min(num) * Max(num);
num = to_string(x);
if (Min(num) == 0) break;
}
printf("%lld \n", x);
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t) {
long long int n, k, product = 1, i = 1, sum, copy, minimum = 10,
maximum = -1;
cin >> n >> k;
copy = sum = n;
for (i = 2; i <= k; i++) {
while (copy > 0) {
minimum = min(minimum, copy % 10);
maximum = max(maximum, copy % 10);
copy /= 10;
}
product = minimum * maximum;
sum = sum + product;
copy = sum;
if (product == 0) break;
minimum = 10;
maximum = -1;
}
cout << sum << endl;
t--;
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long a, k;
cin >> a >> k;
k--;
while (k--) {
long long x = a;
long long mn = 9, mx = 0;
while (x) {
mn = min(x % 10, mn);
mx = max(mx, x % 10);
if (mn == 0 && mx == 9) break;
x /= 10;
}
if (!mn) break;
a = a + mn * mx;
}
cout << a << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
long long n, m;
cin >> n >> m;
long long cnt = 1, temp = n;
while (1) {
if (cnt == m) {
cout << temp << endl;
break;
}
int x = 9, y = 0;
long long t = temp;
while (t != 0) {
int rem = t % 10;
x = min(x, rem);
y = max(y, rem);
t /= 10;
}
cnt++;
temp += (x * y);
if (x * y == 0) {
cout << temp << endl;
break;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 112345;
const int MAXINT = 2147483098;
const long long MAXLL = 9223372036854775258LL;
const long long mod = 1e9 + 9;
long long mind(long long a) {
long long ans = 9;
while (a > 0) {
ans = min(ans, a % 10);
a /= 10;
}
return ans;
}
long long maxd(long long a) {
long long ans = 0;
while (a > 0) {
ans = max(ans, a % 10);
a /= 10;
}
return ans;
}
int main() {
int t;
cin >> t;
long long n, k;
while (t--) {
cin >> n >> k;
for (long long i = 1; i < k; i++) {
int nd = mind(n);
int xd = maxd(n);
n = n + nd * xd;
if (nd == 0) break;
}
cout << n << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int M = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
if (k == 1)
cout << a << '\n';
else {
k--;
while (k--) {
vector<long long int> v;
long long int temp = a;
while (temp) {
v.push_back(temp % 10);
temp = temp / 10;
}
sort(v.begin(), v.end());
if (v[0] == 0) {
break;
} else {
a = a + v[0] * v[v.size() - 1];
}
}
cout << a << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int mul(long long int a) {
int mi = 10, ma = 0;
while (a != 0) {
int d = a % 10;
mi = min(mi, d);
ma = max(ma, d);
a /= 10;
}
return mi * ma;
}
int main() {
int t = 0;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
k--;
while (k--) {
int mult = mul(n);
if (mult == 0) {
break;
}
n += mult;
}
std::cout << n << std::endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long power(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) {
ans = (ans * a);
}
a = (a * a);
b >>= 1;
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
for (long long i = 0; i < k - 1; i++) {
long long minn = 10, maxx = 0;
long long val = n;
while (val) {
minn = min(minn, val % 10);
maxx = max(maxx, val % 10);
val /= 10;
}
if ((maxx * minn) == 0) {
break;
}
n += (maxx * minn);
}
cout << n << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int minn(long long int a) {
long long int min = a;
while (a != 0) {
if (min > a % 10) {
min = a % 10;
}
a /= 10;
}
return min;
}
long long int maxx(long long int a) {
long long int max = 0;
while (a != 0) {
if (max < a % 10) {
max = a % 10;
}
a /= 10;
}
return max;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int a, k;
cin >> a >> k;
long long int i = 1, ans = a;
while (minn(ans) != 0 && i != k) {
i++;
ans += minn(ans) * maxx(ans);
}
cout << ans << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long a, k, temp, min, max, count;
cin >> a >> k;
count = 1;
while (count != k) {
temp = a;
min = temp % 10;
max = temp % 10;
while (temp != 0) {
if (temp % 10 < min) {
min = temp % 10;
}
if (temp % 10 > max) {
max = temp % 10;
}
temp /= 10;
}
if (min == 0) {
break;
}
temp = a + ((min) * (max));
a = temp;
count++;
}
cout << a << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t;
cin >> t;
while (t--) {
ll a, K;
cin >> a >> K;
for (ll i = 1; i < K; ++i) {
ll x = a;
int max_val = -1, min_val = 10;
while (x) {
int y = x % 10;
max_val = max(y, max_val);
min_val = min(y, min_val);
x /= 10;
}
a += max_val * min_val;
if (max_val == 0 || min_val == 0) {
break;
}
}
cout << a << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int v[505];
vector<int> rasp;
void mov(int poz) {
rasp.push_back(poz + 1);
swap(v[poz], v[poz + 1]);
swap(v[poz], v[poz + 2]);
}
int main() {
int t, i1, n, i, j;
scanf("%d", &t);
for (i1 = 1; i1 <= t; i1++) {
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &v[i]);
rasp.clear();
for (i = 2; i < n; i++) {
for (j = 0; j < n - i; j++)
if (v[j] > v[j + 1] && v[j] > v[j + 2]) mov(j);
if (v[n - i] > v[n - i + 1]) mov(n - i - 1);
}
for (i = 0; i < n - 2; i++)
while (v[i] > v[i + 1] || v[i] > v[i + 2]) mov(i);
for (i = 0; i < n - 1; i++)
if (v[i] > v[i + 1]) {
printf("-1\n");
break;
}
if (i == n - 1) {
printf("%d\n", rasp.size());
for (auto it : rasp) printf("%d ", it);
printf("\n");
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 7;
long long n;
vector<long long> a, sorted, res;
void f(long long id) {
res.emplace_back(id + 1);
swap(a[id], a[id + 1]);
swap(a[id], a[id + 2]);
}
void pref(long long beg) {
long long id = 0;
for (long long i = beg; i < n; i++)
if (a[i] == sorted[beg]) id = i;
while (id != beg) {
if (id == beg + 1) {
id++;
f(beg);
} else {
id -= 2;
f(id);
}
}
}
void suf(long long last) {
long long id = 0;
for (long long i = 0; i <= last; i++)
if (a[i] == sorted[last]) id = i;
while (id != last) {
if (id == last - 1)
f(id - 1);
else
f(id);
id++;
}
}
void solve() {
a.clear();
sorted.clear();
res.clear();
cin >> n;
a.resize(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
sorted.emplace_back(a[i]);
}
sort(sorted.begin(), sorted.end());
long long last_id = a.size() - 2;
for (long long i = 0; i < a.size() - 1; i++)
if (sorted[i] == sorted[i + 1]) last_id = i;
for (long long i = 0; i < last_id; i++) pref(i);
for (long long i = n - 1; i >= last_id + 2; i--) suf(i);
if (a[n - 1] < a[n - 2])
cout << -1 << endl;
else {
cout << res.size() << endl;
for (long long i : res) cout << i << " ";
cout << endl;
}
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
long long t;
cin >> t;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 500;
const int mod = 1e9 + 7;
int T, n;
int a[maxn];
vector<int> ans;
void swift(int x) {
int tmp = a[x + 2];
a[x + 2] = a[x + 1];
a[x + 1] = a[x];
a[x] = tmp;
}
bool slove() {
ans.clear();
for (int i = 1; i <= n - 2; i++) {
int mn = INT_MAX, id;
for (int j = i; j <= n; j++) {
if (a[j] < mn) mn = a[j], id = j;
}
while (id - 2 >= i) id -= 2, swift(id), ans.push_back(id);
if (id - 1 == i) swift(i), swift(i), ans.push_back(i), ans.push_back(i);
}
for (int i = n; i >= 3; i--) {
int mx = -1, id;
for (int j = i; j >= 1; j--) {
if (a[j] > mx) mx = a[j], id = j;
}
if (id == i - 1) swift(id - 1), ans.push_back(id - 1);
}
for (int i = 1; i < n; i++) {
if (a[i] > a[i + 1]) return false;
}
return true;
}
int main() {
cin >> T;
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
if (!slove())
puts("-1");
else {
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
puts("");
}
}
}
|
#include <bits/stdc++.h>
const long long INF = (long long)1e9;
using namespace std;
long long power(long long x, long long y) {
long long res = 1;
x = x % 998244353;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = ((res % 998244353) * (x % 998244353)) % 998244353;
y = y >> 1;
x = ((x % 998244353) * (x % 998244353)) % 998244353;
}
return res;
}
vector<long long> pr;
void sieve(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long i = 2; i <= n; i++)
if (prime[i]) pr.push_back(i);
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long dp[1005][1005];
void reduce_to_zero() {
long long range = 100, add = 0;
long long x = 1;
while (range) {
add += min(range, x);
x;
range -= min(x, range);
}
}
const long long N = 1e6 + 100;
long long fact[N], modulo = INF + 7;
long long binpow(long long val, long long deg, long long modi) {
if (!deg) return 1 % modi;
if (deg & 1) return binpow(val, deg - 1, 998244353) * val % modi;
long long res = binpow(val, deg >> 1, modi);
return (res * res) % modi;
}
void initfact() {
fact[0] = 1;
for (long long i = 1; i < N; i++) {
fact[i] = (fact[i - 1] * i);
fact[i] %= modulo;
}
}
long long getC(long long n, long long i) {
long long res = fact[n];
long long div = fact[n - i] * fact[i];
div %= modulo;
div = binpow(div, modulo - 2, modulo);
return (res * div) % modulo;
}
long long n, arr[505], arr2[505], tmp[505];
vector<long long> ans;
void shift(long long idx) {
ans.push_back(idx);
tmp[idx] = arr[idx + 2], tmp[idx + 1] = arr[idx], tmp[idx + 2] = arr[idx + 1];
for (long long j = idx; j <= idx + 2; j++) arr[j] = tmp[j];
}
void we_have_choices() {
ans.clear();
cin >> n;
for (long long i = 1; i <= n; i++) cin >> arr[i], arr2[i] = arr[i];
sort(arr2 + 1, arr2 + 1 + n);
for (long long i = 1; i <= n; i++) {
long long idx = i;
while (arr[idx] != arr2[i]) idx++;
while (idx != i) {
while (idx - 2 >= i) {
idx -= 2;
shift(idx);
}
if (idx == i + 1) {
if (idx != n) {
idx--;
shift(idx);
shift(idx);
} else {
if (arr[n - 2] == arr[n]) {
shift(n - 2);
break;
}
long long j;
for (j = n - 2; j >= 1; j--) {
if (arr[j] == arr[j + 1]) break;
}
if (!j) {
cout << -1 << '\n';
return;
} else {
for (long long k = j; k <= n - 2; k++) {
shift(k);
shift(k);
}
}
idx = i;
}
}
}
}
cout << ans.size() << '\n';
for (auto i : ans) cout << i << " ";
cout << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
;
long long t;
cin >> t;
while (t--) we_have_choices();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
auto make = [](vector<int> &a, int pos) {
swap(a[pos + 1], a[pos + 2]);
swap(a[pos], a[pos + 1]);
};
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
vector<pair<int, int>> res(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
res[i] = {a[i], i};
}
sort(res.begin(), res.end());
for (int i = 0; i < n; ++i) {
a[res[i].second] = i;
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
cnt += a[j] < a[i];
}
}
if (cnt & 1) {
for (int i = 0; i < n - 1; ++i) {
if (res[i].first == res[i + 1].first) {
swap(a[res[i].second], a[res[i + 1].second]);
break;
}
}
}
vector<int> ans;
for (int i = 0; i < n - 2; ++i) {
int pos = min_element(a.begin() + i, a.end()) - a.begin();
while (pos > i + 1) {
make(a, pos - 2);
ans.push_back(pos - 2);
pos -= 2;
}
if (pos != i) {
make(a, i);
make(a, i);
ans.push_back(i);
ans.push_back(i);
pos = i;
}
}
for (int i = 0; i < 3; ++i) {
if (is_sorted(a.begin(), a.end())) {
break;
}
make(a, n - 3);
ans.push_back(n - 3);
}
if (!is_sorted(a.begin(), a.end())) {
cout << -1 << endl;
} else {
cout << ans.size() << endl;
for (auto it : ans) cout << it + 1 << " ";
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
const int oo = 1e9 + 20;
const int MOD = 998244353;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a;
set<int> ve;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
ve.insert(x);
a.push_back(x);
}
vector<int> ans;
for (int i = 0; i < n - 2; ++i) {
pair<int, int> mn = {oo, -1};
for (int j = i; j < n; ++j) mn = min(mn, {a[j], j});
while (mn.second > i) {
if (mn.second - 2 >= i) {
swap(a[mn.second], a[mn.second - 2]);
swap(a[mn.second], a[mn.second - 1]);
ans.push_back(mn.second - 1);
mn.second -= 2;
} else {
swap(a[mn.second], a[mn.second - 1]);
swap(a[mn.second], a[mn.second + 1]);
ans.push_back(mn.second);
ans.push_back(mn.second);
mn.second -= 1;
}
}
}
int ok = (a[n - 2] > a[n - 1] && ve.size() == n);
if (ok) {
cout << -1 << '\n';
} else {
if (a[n - 2] > a[n - 1]) {
int cur = n - 2;
while (a[cur - 1] != a[cur + 1]) {
ans.push_back(cur);
swap(a[cur + 1], a[cur - 1]);
swap(a[cur], a[cur + 1]);
cur--;
}
ans.push_back(cur);
}
ok = (ans.size() <= n * n);
if (ok) {
cout << ans.size() << '\n';
for (auto i : ans) cout << i << ' ';
cout << '\n';
} else
cout << -1 << '\n';
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6 + 8;
const long long MOD = 1e9 + 7;
const long long sz = 1e5 + 5;
void f_io() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
}
int32_t main() {
f_io();
long long tc = 1;
cin >> tc;
auto rotate = [](vector<long long>& v, long long ind) {
swap(v[ind + 1], v[ind + 2]);
swap(v[ind], v[ind + 1]);
};
while (tc--) {
long long n;
cin >> n;
vector<pair<long long, long long>> val;
for (long long i = 0, a; i < n; i++) {
cin >> a;
val.push_back({a, i});
}
sort(begin(val), end(val));
vector<long long> permutation(n);
long long inversion_cnt = 0;
for (long long i = 0; i < n; i++) permutation[val[i].second] = i;
for (long long i = 0; i < n; i++) {
for (long long j = i + 1; j < n; j++) {
if (permutation[j] < permutation[i]) inversion_cnt++;
}
}
long long poss = 1;
if (inversion_cnt & 1) {
poss = 0;
for (long long i = 0; i < n - 1; i++) {
if (val[i].first == val[i + 1].first) {
swap(permutation[val[i].second], permutation[val[i + 1].second]);
poss = 1;
break;
}
}
}
vector<long long> ret;
if (!poss) {
cout << "-1"
<< "\n";
continue;
}
for (long long i = 0; i < n - 2; i++) {
long long ind = min_element(begin(permutation) + i, end(permutation)) -
permutation.begin();
while (ind > i + 1) {
rotate(permutation, ind - 2);
ret.push_back(ind - 2);
ind -= 2;
}
if (ind != i) {
rotate(permutation, i);
ret.push_back(i);
ret.push_back(i);
rotate(permutation, i);
ind = i;
}
}
for (long long i = 0; i < 3; i++) {
if (is_sorted(begin(permutation), end(permutation))) break;
rotate(permutation, n - 3);
ret.push_back(n - 3);
}
if (!is_sorted(begin(permutation), end(permutation)))
cout << "-1"
<< "\n";
else {
cout << ret.size() << "\n";
for (auto k : ret) cout << k + 1 << " ";
cout << "\n";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> A(n, 0);
for (int i(0), _n(n); i < _n; ++i) cin >> A[i];
vector<int> steps;
for (int i(0), _n(n - 2); i < _n; ++i) {
int mn = min_element(A.begin() + i, A.end()) - A.begin();
if (mn == i) continue;
if (mn == n - 1) {
int a = mn - 2, b = mn - 1, c = mn;
swap(A[b], A[c]);
swap(A[a], A[b]);
steps.push_back(a);
mn = a;
}
if ((mn - i) % 2) {
int a = mn - 1, b = mn, c = mn + 1;
swap(A[b], A[c]);
swap(A[a], A[b]);
steps.push_back(a);
mn = c;
}
while (mn != i) {
int a = mn - 2, b = mn - 1, c = mn;
swap(A[b], A[c]);
swap(A[a], A[b]);
steps.push_back(a);
mn = a;
}
}
int i = n - 1;
while (i >= 2) {
if (A[i] >= A[i - 1] && A[i - 1] >= A[i - 2]) break;
int a = i - 2, b = i - 1, c = i;
swap(A[b], A[c]);
swap(A[a], A[b]);
steps.push_back(a);
i--;
}
if (i == 1) i++;
if (A[i] >= A[i - 1] && A[i - 1] >= A[i - 2]) {
printf("%d\n", int(steps.size()));
for (int i(0), _n(steps.size()); i < _n; ++i) printf("%d ", steps[i] + 1);
printf("\n");
} else
printf("-1\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
const int N = 500 + 10;
int A[N], a[N], b[N], c[N];
vector<pair<int, int> > vec;
vector<int> ans;
void swp(int i, int j) {
swap(c[b[i]], c[b[j]]);
swap(a[i], a[j]);
swap(b[i], b[j]);
}
void upd(int i) {
ans.push_back(i);
swp(i + 1, i + 2);
swp(i, i + 1);
}
bool solve(int n) {
vec.clear();
ans.clear();
for (int i = 1; i <= n; i++) {
vec.push_back({a[i], i});
A[i] = a[i];
}
sort(vec.begin(), vec.end());
for (int i = 0; i < n; i++) {
b[vec[i].second] = i + 1;
c[i + 1] = vec[i].second;
}
bool ok = 1;
for (int i = n; i >= 2; i--) {
if (i == 2 && c[i] != 2) {
ok = 0;
break;
}
while (c[i] < i) {
upd(max(1, c[i] - 1));
}
}
if (ok) return 1;
for (int i = 0; i < n - 1; i++) {
if (vec[i].first == vec[i + 1].first) {
swap(vec[i], vec[i + 1]);
break;
}
}
for (int i = 0; i < n; i++) {
b[vec[i].second] = i + 1;
c[i + 1] = vec[i].second;
}
for (int i = 1; i <= n; i++) {
a[i] = A[i];
}
ans.clear();
ok = 1;
for (int i = n; i >= 2; i--) {
if (i == 2 && c[i] != 2) {
ok = 0;
break;
}
while (c[i] < i) {
upd(max(1, c[i] - 1));
}
}
if (ok) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
if (solve(n)) {
cout << ans.size() << "\n";
for (int j : ans) {
cout << j << " ";
}
cout << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll power(ll x, ll y);
const ll MOD = 1000000007;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n + 5];
for (int i = 1; i <= n; i++) cin >> arr[i];
vector<int> res;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 2]);
swap(arr[j], arr[j + 1]);
res.emplace_back(j);
res.emplace_back(j);
} else if (arr[j + 1] > arr[j + 2]) {
swap(arr[j], arr[j + 1]);
swap(arr[j], arr[j + 2]);
res.emplace_back(j);
}
}
}
int flag = 0;
for (int i = 2; i <= n; i++)
if (arr[i] < arr[i - 1]) flag = 1;
if (flag)
cout << -1 << "\n";
else {
cout << res.size() << "\n";
for (auto i : res) cout << i << "\n";
cout << "\n";
}
}
}
ll power(ll x, ll y) {
ll res = 1;
x %= MOD;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1, x = (x * x) % MOD;
}
return res;
}
|
#include <bits/stdc++.h>
using namespace std;
long long n = 0;
long long arr[501];
void reset() {}
void init() {}
pair<bool, vector<long long>> solve_vector(
vector<pair<long long, long long>> v) {
vector<long long> res;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < v.size(); j++) {
if (v[j].second > j) {
while (v[j].second - j >= 2) {
res.push_back(v[j].second - 2);
long long ind1 = 0, ind2 = 0;
long long ind3 = j;
for (long long k = 0; k < v.size(); k++) {
if (v[k].second == v[j].second - 2) ind1 = k;
if (v[k].second == v[j].second - 1) ind2 = k;
}
v[ind1].second++;
v[ind2].second++;
v[ind3].second -= 2;
}
if (v[j].second - j == 1) {
res.push_back(v[j].second - 1);
res.push_back(v[j].second - 1);
long long ind1 = -1, ind2 = j;
long long ind3 = -1;
for (long long k = 0; k < v.size(); k++) {
if (v[k].second == v[j].second - 1) ind1 = k;
if (v[k].second == v[j].second + 1) ind3 = k;
}
if (ind3 == -1) return {false, vector<long long>()};
v[ind1].second = j + 2;
v[ind3].second = j + 1;
v[j].second = j;
}
break;
}
}
}
return {true, res};
}
void solve() {
vector<pair<long long, long long>> v1, v2;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
v1.push_back({arr[i], i});
v2.push_back({arr[i], i});
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
bool flag = false;
for (long long i = 0; i < v2.size(); i++) {
for (long long j = 0; j < v2.size(); j++)
if (i != j && v2[i].first == v2[j].first) {
swap(v2[i], v2[j]);
flag = true;
break;
}
if (flag) break;
}
pair<bool, vector<long long>> p1 = solve_vector(v1);
pair<bool, vector<long long>> p2 = solve_vector(v2);
if (p1.first) {
cout << p1.second.size() << endl;
for (long long i = 0; i < p1.second.size(); i++)
cout << p1.second[i] + 1 << ' ';
cout << endl;
} else if (p2.first) {
cout << p2.second.size() << endl;
for (long long i = 0; i < p2.second.size(); i++)
cout << p2.second[i] + 1 << ' ';
cout << endl;
} else
cout << -1 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
init();
long long t = 0;
cin >> t;
while (t--) {
solve();
reset();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
const int mv = 501;
int t, iar[mv], ar[mv], par[mv], sar[mv];
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> ar[i];
iar[i] = par[i] = sar[i] = i;
}
sort(sar, sar + n, [&](int a, int b) { return ar[a] < ar[b]; });
vector<int> ana;
for (int i = 0; i < n - 2; ++i) {
int ti = iar[sar[i]];
while (ti > 1 and i != ti and ti - 2 >= i) {
int tm = par[ti];
iar[tm] -= 2;
iar[par[ti - 1]]++;
iar[par[ti - 2]]++;
par[ti] = par[ti - 1];
par[ti - 1] = par[ti - 2];
par[ti - 2] = tm;
ti -= 2;
ana.push_back(ti + 1);
}
if (i != ti and ti - 1 == i) {
ana.push_back(ti);
ana.push_back(ti);
iar[par[ti - 1]] += 2;
iar[par[ti]]--;
iar[par[ti + 1]]--;
int tm = par[ti];
par[ti] = par[ti + 1];
par[ti + 1] = par[ti - 1];
par[ti - 1] = tm;
}
if (ana.size() > n * n) goto np;
}
if (iar[sar[n - 2]] != n - 2) {
int dv = -1;
for (int i = n - 1; i > 0; --i)
if (ar[sar[i]] == ar[sar[i - 1]]) {
dv = i;
break;
}
if (dv == -1) goto np;
int k = n - 2;
while (k >= dv) ana.push_back(k--);
}
if (ana.size() > n * n) {
np:
cout << "-1\n";
continue;
}
cout << ana.size() << '\n';
for (int i = 0; i < ana.size(); ++i) cout << ana[i] << " ";
cout << '\n';
}
}
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int maxn = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n;
int a[maxn];
struct P {
int x, id;
bool operator<(const P &rhs) const {
if (x == rhs.x) return id < rhs.id;
return x < rhs.x;
}
} p[maxn], q[maxn];
void solve() {
n = read();
map<int, int> vis;
int cnt = 0, f = 0;
for (int i = 1; i <= n; ++i) {
a[i] = read();
for (int j = 1; j < i; ++j) cnt += a[j] > a[i];
if (vis[a[i]])
f = 1;
else
vis[a[i]]++;
}
if (cnt % 2 && !f)
printf("-1\n");
else {
int ok = 1;
for (int i = 1; i < n; ++i)
if (a[i] > a[i + 1]) {
ok = 0;
break;
}
if (ok) return (void)printf("0\n\n");
vector<int> ans;
if (cnt % 2) {
vis.clear();
for (int i = 1; i <= n; ++i) {
if (vis[a[i]]) {
while (i > 2 && a[i - 2] != a[i] && a[i - 1] != a[i]) {
ans.push_back(i - 2);
int t = a[i];
a[i] = a[i - 1];
a[i - 1] = a[i - 2];
a[i - 2] = t;
i -= 2;
}
if (i == 2) {
while (a[i] == a[i + 1]) i++;
ans.push_back(i - 1);
ans.push_back(i - 1);
int t = a[i - 1];
a[i - 1] = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
} else {
ans.push_back(i - 2);
int t = a[i];
a[i] = a[i - 1];
a[i - 1] = a[i - 2];
a[i - 2] = t;
i -= 2;
}
break;
} else
vis[a[i]]++;
}
}
for (int i = 1; i <= n; ++i) p[i].x = a[i], p[i].id = i;
sort(p + 1, p + 1 + n);
for (int i = 1; i <= n; ++i) a[p[i].id] = i;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (a[j] == i) {
while (j - a[j] >= 2) {
ans.push_back(j - 2);
int t = a[j];
a[j] = a[j - 1];
a[j - 1] = a[j - 2];
a[j - 2] = t;
j -= 2;
}
if (j != a[j]) {
assert(j == a[j] + 1);
ans.push_back(j - 1);
int t = a[j + 1];
a[j + 1] = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
j++;
ans.push_back(j - 2);
t = a[j];
a[j] = a[j - 1];
a[j - 1] = a[j - 2];
a[j - 2] = t;
j -= 2;
}
break;
}
}
}
printf("%d\n", int(ans.size()));
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
puts("");
}
}
int main() {
int T = read();
while (T--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const long long inf = 1e18;
long long i, j, k, n, m, x, y, ma = -inf, mi = inf, qq = 1, w, frr, to, t, ppp;
char ch;
vector<long long> a;
void solve() {
cin >> n;
a.resize(n);
vector<pair<long long, long long> > b(n);
for (i = 0; i < n; i++) {
cin >> a[i];
b[i] = {a[i], i};
}
if (ppp == 53) {
}
sort((b).begin(), (b).end());
long long cnt = -1;
for (i = 0; i < n; i++) {
if (i == 0 || b[i].first != b[i - 1].first) cnt++;
a[b[i].second] = cnt;
}
for (i = 0; i < n; i++) {
b[i].first = a[i];
}
sort((b).begin(), (b).end());
vector<long long> ans;
long long t1, t2, t3;
for (i = 0; i < n - 2; i++) {
if (a[i] == b[i].first) continue;
long long it;
for (j = i + 1; j < n; j++)
if (a[j] == b[i].first) {
it = j;
break;
}
while (it - i > 1) {
ans.push_back(it - 2);
t1 = a[it - 2];
t2 = a[it - 1];
t3 = a[it];
a[it] = t2;
a[it - 1] = t1;
a[it - 2] = t3;
it -= 2;
}
if (it != i) {
ans.push_back(i);
ans.push_back(i);
swap(a[i], a[i + 2]);
swap(a[i], a[i + 1]);
}
}
if (a[n - 1] == a[n - 3]) {
ans.push_back(n - 3);
swap(a[n - 2], a[n - 1]);
}
if (a[n - 1] == b[n - 1].first) {
cout << ans.size() << '\n';
for (auto v : ans) cout << v + 1 << ' ';
cout << '\n';
} else {
if (n == 3 && a[0] == a[2]) {
ans.push_back(0);
cout << ans.size() << '\n';
for (auto v : ans) cout << v + 1 << ' ';
cout << '\n';
return;
}
if (cnt == n - 1 || n < 4) {
cout << "-1\n";
return;
}
long long it;
for (i = n - 3; i > 0; i--) {
if (b[i].first == b[i - 1].first) {
it = i - 1;
break;
}
}
long long fit = it;
while (it < n - 4) {
ans.push_back(it);
swap(a[it], a[it + 2]);
it++;
}
ans.push_back(n - 4);
ans.push_back(n - 4);
ans.push_back(n - 3);
ans.push_back(n - 3);
it = n - 5;
while (it >= fit) {
ans.push_back(it);
ans.push_back(it);
it--;
}
cout << ans.size() << '\n';
for (auto v : ans) cout << v + 1 << ' ';
cout << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> qq;
for (ppp = 1; ppp <= qq; ppp++) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long a[505], ar[505], num[505], b[505];
bool used[505];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> ans;
memset(num, 0, sizeof(num));
memset(used, 0, sizeof(used));
bool swi = false;
long long swa1, swa2;
for (long long i = 0; i < n; i++) cin >> ar[i];
for (long long i = 0; i < n; i++) {
if (num[ar[i]]) {
swi = true;
swa1 = num[ar[i]] - 1, swa2 = i;
break;
}
num[ar[i]] = i + 1;
}
for (long long i = 0; i < n; i++) {
long long p, mi = 501;
for (long long j = 0; j < n; j++)
if (!used[j] && ar[j] < mi) mi = ar[j], p = j;
a[p] = i + 1;
b[p] = i + 1;
used[p] = true;
}
long long N = n;
while (n > 3) {
long long p, ma = 0;
for (long long i = 0; i < n; i++)
if (a[i] > ma) ma = a[i], p = i;
while (p < n - 3) {
ans.push_back(p + 1);
long long tem = a[p + 2];
a[p + 2] = a[p + 1];
a[p + 1] = a[p];
a[p] = tem;
p++;
}
while (p < n - 1) {
ans.push_back(n - 2);
long long tem = a[n - 1];
a[n - 1] = a[n - 2];
a[n - 2] = a[n - 3];
a[n - 3] = tem;
p++;
}
n--;
}
bool tag = false;
for (long long i = 0; i < 3; i++) {
if (a[0] <= a[1] && a[1] <= a[2]) {
tag = true;
break;
}
ans.push_back(1);
long long tem = a[2];
a[2] = a[1];
a[1] = a[0];
a[0] = tem;
}
if (tag) {
cout << ((long long)ans.size()) << '\n';
for (long long i : ans) cout << i << " ";
cout << '\n';
continue;
}
if (!swi) {
cout << "-1" << '\n';
continue;
}
ans.clear();
n = N;
swap(b[swa1], b[swa2]);
for (long long i = 0; i < n; i++) a[i] = b[i];
while (n > 3) {
long long p, ma = 0;
for (long long i = 0; i < n; i++)
if (a[i] > ma) ma = a[i], p = i;
while (p < n - 3) {
ans.push_back(p + 1);
long long tem = a[p + 2];
a[p + 2] = a[p + 1];
a[p + 1] = a[p];
a[p] = tem;
p++;
}
while (p < n - 1) {
ans.push_back(n - 2);
long long tem = a[n - 1];
a[n - 1] = a[n - 2];
a[n - 2] = a[n - 3];
a[n - 3] = tem;
p++;
}
n--;
}
for (long long i = 0; i < 3; i++) {
if (a[0] <= a[1] && a[1] <= a[2]) {
tag = true;
break;
}
ans.push_back(1);
long long tem = a[2];
a[2] = a[1];
a[1] = a[0];
a[0] = tem;
}
if (tag) {
cout << ((long long)ans.size()) << '\n';
for (long long i : ans) cout << i << " ";
cout << '\n';
} else
cout << "-1" << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int T, n, mk, a[N];
vector<int> res;
void flip(int x) {
int t = a[x + 2];
a[x + 2] = a[x + 1];
a[x + 1] = a[x];
a[x] = t;
}
void solve(int x, int y) {
if (a[x] == a[x + 2]) {
flip(x);
flip(x);
res.push_back(x);
res.push_back(x);
return;
}
if (y >= n - 1) {
mk = 0;
return;
}
if (a[x + 2] == a[y + 2]) {
flip(y);
flip(x);
res.push_back(y);
res.push_back(x);
return;
}
solve(x + 1, y + 1);
flip(x);
res.push_back(x);
}
int main() {
scanf("%d", &T);
while (T--) {
res.clear();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= 3; i--) {
int pos = 0, imax = 0;
for (int k = 1; k <= i; k++) {
if (a[k] > imax) {
imax = a[k];
pos = k;
}
}
if (pos == i) continue;
for (int k = 0; k < i - pos - 1; k++) {
res.push_back(pos + k);
flip(pos + k);
}
flip(i - 2);
res.push_back(i - 2);
}
if (a[1] > a[2]) {
mk = 1;
solve(1, 2);
if (0 == mk) {
printf("-1\n");
continue;
}
}
printf("%d\n", res.size());
for (int i = 0; i < res.size(); i++) {
printf("%d", res[i]);
printf(i == res.size() - 1 ? "\n" : " ");
}
if (0 == res.size()) printf("\n");
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.