Codeforces Rund 976 div2 个人题解(A~E)

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) (nkx),其中 x x x是非负整数。

找出将 n n n变为 0 0 0所需的最小操作次数。

输入

每个测试包含多个测试用例。第一行是测试用例的数量 t t t( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104)。后面是每个测试用例的描述。

每个测试用例的唯一一行包含两个整数 n n n k k k( 1 ≤ n , k ≤ 1 0 9 1 \le n, k \le 10^9 1n,k109)。

输出

对于每个测试用例,输出每行所需的最小操作次数。

示例

输入

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。我们可以执行以下操作序列:

  1. 5 5 5中减去 2 0 = 1 2^0 = 1 20=1,当前值变为 5 − 1 = 4 5 - 1 = 4 51=4
  2. 4 4 4中减去 2 2 = 4 2^2 = 4 22=4,当前值变为 4 − 4 = 0 4 - 4 = 0 44=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。我们可以执行以下操作序列:

  1. 3 3 3中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 3 − 1 = 2 3 - 1 = 2 31=2
  2. 2 2 2中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 2 − 1 = 1 2 - 1 = 1 21=1
  3. 1 1 1中减去 5 0 = 1 5^0 = 1 50=1,当前值变为 1 − 1 = 0 1 - 1 = 0 11=0

可以证明在少于 3 3 3次操作的情况下无法将 n n n变为 0 0 0。因此,答案是 3 3 3

解题思路
  1. 特殊情况处理

    • k = 1 k = 1 k=1时,我们只能减去 1 1 1(即 1 0 1^0 10),因此将 n n n减到 0 0 0需要 n n n次操作。
  2. 计算 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),并将这些值存储在一个列表中。
  3. 贪心算法

    • 从最大的 k x k^x kx开始,优先使用较大的幂来减少 n n n,每次减去 k x k^x kx的最大倍数,直到无法再减去为止。
    • 继续使用下一个较小的 k x k^x k
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值