目录
1.Problem
Karl likes Codeforces and subsequences. He wants to find a string of lowercase English letters that contains at least kk subsequences codeforces. Out of all possible strings, Karl wants to find a shortest one.
Formally, a codeforces subsequence of a string ss is a subset of ten characters of ss that read codeforces from left to right. For example, codeforces contains codeforces a single time, while codeforcesisawesome contains codeforces four times: codeforcesisawesome, codeforcesisawesome, codeforcesisawesome, codeforcesisawesome.
Help Karl find any shortest string that contains at least kk codeforces subsequences.
2.Input
The only line contains a single integer kk (1≤k≤1016)1≤k≤1016).
3.Output
Print a shortest string of lowercase English letters that contains at least kk codeforces subsequences. If there are several such strings, print any of them.
4.Examples
4.1input
1
4.2output
codeforces
5.Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; }
template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; }
ll readint() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
ll n;
char s[11] = {' ', 'c', 'o', 'd', 'e', 'f', 'o', 'r', 'c', 'e', 's'};
ll qpow(ll x, ll p) {
ll ret = 1;
for (; p; p >>= 1, x = x * x)
if (p & 1) ret = ret * x;
return ret;
}
int main() {
n = readint();
ll x = 0;
for (int i = 1; i <= 100; i++) {
if (qpow(i, 10) <= n) x = i;
else break;
}
ll t = qpow(x, 10), cnt = 0;
while (t < n) {
cnt++;
t = t / x * (x + 1);
}
for (int i = 1; i <= cnt; i++)
for (int j = 1; j <= x + 1; j++)
printf("%c", s[i]);
for (int i = cnt + 1; i <= 10; i++)
for (int j = 1; j <= x; j++)
printf("%c", s[i]);
printf("\n");
return 0;
}
6.Conclusion
这段代码的作用是根据给定的整数n,生成一个字符串序列。字符串的构造方式是通过一定的规则不断重复某个子串。具体而言,代码的逻辑如下:
- 通过
readint
函数读取一个整数n。- 通过循环找到一个整数x,使得x的10次方小于等于n。这个x用于构造字符串序列。
- 利用
qpow
函数计算x的10次方,并将其赋值给变量t。- 使用循环,不断对t进行操作,直到t大于等于n。在每次循环中,操作包括将t除以x,然后乘以(x+1)。同时,记录循环的次数,即cnt。
- 根据循环的次数cnt和x的值,按照预定的规则构造字符串序列,输出到标准输出。
总体而言,这段代码的目的是根据规定的数学逻辑生成一个字符串序列,输出到标准输出。生成的字符串序列由一系列特定的子串组成,子串的选择和重复次数依赖于输入的整数n。