Codeforces Rund 976 div2 个人题解(A~E)
Dashboard - Codeforces Round 976 (Div. 2) and Divide By Zero 9.0 - Codeforces
火车头
#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <chrono>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define ft first
#define sd second
#define yes cout << "yes\n"
#define no cout << "no\n"
#define Yes cout << "Yes\n"
#define No cout << "No\n"
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define unq_all1(x) x.erase(unique(all1(x)), x.end())
#define sort_all(x) sort(all(x))
#define sort1_all(x) sort(all1(x))
#define reverse_all(x) reverse(all(x))
#define reverse1_all(x) reverse(all1(x))
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3fLL
#define RED cout << "\033[91m"
#define GREEN cout << "\033[92m"
#define YELLOW cout << "\033[93m"
#define BLUE cout << "\033[94m"
#define MAGENTA cout << "\033[95m"
#define CYAN cout << "\033[96m"
#define RESET cout << "\033[0m"
// 红色
#define DEBUG1(x) \
RED; \
cout << #x << " : " << x << endl; \
RESET;
// 绿色
#define DEBUG2(x) \
GREEN; \
cout << #x << " : " << x << endl; \
RESET;
// 蓝色
#define DEBUG3(x) \
BLUE; \
cout << #x << " : " << x << endl; \
RESET;
// 品红
#define DEBUG4(x) \
MAGENTA; \
cout << #x << " : " << x << endl; \
RESET;
// 青色
#define DEBUG5(x) \
CYAN; \
cout << #x << " : " << x << endl; \
RESET;
// 黄色
#define DEBUG6(x) \
YELLOW; \
cout << #x << " : " << x << endl; \
RESET;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t i128;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef pair<ll, int> pli;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<string, ll> psl;
typedef tuple<int, int, int> ti3;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ld, ld, ld> tld3;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pli> vpli;
typedef vector<pss> vpss;
typedef vector<ti3> vti3;
typedef vector<tl3> vtl3;
typedef vector<tld3> vtld3;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef queue<int> qi;
typedef queue<ll> ql;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef queue<psi> qpsi;
typedef queue<psl> qpsl;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pql;
typedef priority_queue<string> pqs;
typedef priority_queue<pii> pqpii;
typedef priority_queue<psi> pqpsi;
typedef priority_queue<pll> pqpll;
typedef priority_queue<psi> pqpsl;
typedef map<int, int> mii;
typedef map<int, bool> mib;
typedef map<ll, ll> mll;
typedef map<ll, bool> mlb;
typedef map<char, int> mci;
typedef map<char, ll> mcl;
typedef map<char, bool> mcb;
typedef map<string, int> msi;
typedef map<string, ll> msl;
typedef map<int, bool> mib;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> uml;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, ll> umcl;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, ll> umsl;
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T read()
{
T x = 0;
int y = 1;
char ch = getchar();
while (ch > '9' || ch < '0')
{
if (ch == '-')
y = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * y;
}
template <typename T>
inline void write(T x)
{
if (x < 0)
{
putchar('-');
x = -x;
}
if (x >= 10)
{
write(x / 10);
}
putchar(x % 10 + '0');
}
/*#####################################BEGIN#####################################*/
void solve()
{
}
int main()
{
ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
int _ = 1;
std::cin >> _;
while (_--)
{
solve();
}
return 0;
}
/*######################################END######################################*/
// 链接:
A. Find Minimum Operations
每个测试的时间限制:1秒
每个测试的内存限制:256兆字节
输入:标准输入
输出:标准输出
给定两个整数 n n n和 k k k。
在一次操作中,你可以从 n n n中减去任何 k k k的幂。具体来说,在一次操作中,你可以将 n n n替换为 ( n − k x ) (n - k^x) (n−kx),其中 x x x是非负整数。
找出将 n n n变为 0 0 0所需的最小操作次数。
输入
每个测试包含多个测试用例。第一行是测试用例的数量 t t t( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104)。后面是每个测试用例的描述。
每个测试用例的唯一一行包含两个整数 n n n和 k k k( 1 ≤ n , k ≤ 1 0 9 1 \le n, k \le 10^9 1≤n,k≤109)。
输出
对于每个测试用例,输出每行所需的最小操作次数。
示例
输入
6
5 2
3 5
16 4
100 3
6492 10
10 1
输出
2
3
1
4
21
10
注意
在第一个测试用例中, n = 5 n = 5 n=5和 k = 2 k = 2 k=2。我们可以执行以下操作序列:
- 从 5 5 5中减去 2 0 = 1 2^0 = 1 20=1,当前值变为 5 − 1 = 4 5 - 1 = 4 5−1=4。
- 从 4 4 4中减去 2 2 = 4 2^2 = 4 22=4,当前值变为 4 − 4 = 0 4 - 4 = 0 4−4=0。
可以证明在少于 2 2 2次操作的情况下无法将 n n n变为 0 0 0。因此,答案是 2 2 2。
在第二个测试用例中, n = 3 n = 3 n=3和 k = 5 k = 5 k=5。我们可以执行以下操作序列:
- 从 3 3 3中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 3 − 1 = 2 3 - 1 = 2 3−1=2。
- 从 2 2 2中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 2 − 1 = 1 2 - 1 = 1 2−1=1。
- 从 1 1 1中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 1 − 1 = 0 1 - 1 = 0 1−1=0。
可以证明在少于 3 3 3次操作的情况下无法将 n n n变为 0 0 0。因此,答案是 3 3 3。
解题思路
-
特殊情况处理:
- 当 k = 1 k = 1 k=1时,我们只能减去 1 1 1(即 1 0 1^0 10),因此将 n n n减到 0 0 0需要 n n n次操作。
-
计算 k k k的幂:
- 对于 k > 1 k > 1 k>1,我们需要计算出所有可能的 k x k^x kx(直到 k x k^x kx超过 1 0 9 10^9 109),并将这些值存储在一个列表中。
-
贪心算法:
- 从最大的 k x k^x kx开始,优先使用较大的幂来减少 n n n,每次减去 k x k^x kx的最大倍数,直到无法再减去为止。
- 继续使用下一个较小的 k x k^x k