题目:给定一个串“codeforces”,给定一个n,让你在原串上任意位置添加任意个数的字符构造出一个字符串s,使得至少有n组子序列能够组成“codeforces”,需要构造出的串长度最短,再输出构造出的s
思路:根据排列组合的性质,我们尽量平均“codeforces”上每位的个数即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <queue> 5 #include <string> 6 #include <vector> 7 #include <cmath> 8 #include <stack> 9 #include <map> 10 11 using namespace std; 12 13 #define ll long long 14 #define pb push_back 15 #define fi first 16 #define se second 17 18 const int N = 2e6 + 10; 19 20 21 void solve() 22 { 23 ll n; 24 scanf("%lld", &n); 25 ll p = 1; 26 vector<ll > Pow; 27 while(1){ 28 ll k = p*p*p*p*p*p*p*p*p*p; 29 Pow.pb(k); 30 p++; 31 if(k > 1e16) break; 32 } 33 int _size = Pow.size(); 34 int inx = -1; 35 for(int i = 0; i < _size; ++i){ 36 if(Pow[i] > n) break; 37 //找到满足 i^10 <= n < (i+1)^10 的i 38 inx = i; 39 } 40 // cout << "inx = " << inx << endl; 41 // cout << "pow = " << Pow[inx] << endl; 42 inx++; 43 44 //下面确定几个字符需要比平均多一个 45 //---------------------------- 46 ll a[20], b[20]; 47 a[0] = 1; 48 a[1] = inx; 49 for(int i = 2; i <= 10; ++i) a[i] = a[i - 1] * inx; 50 b[0] = 1; 51 b[1] = inx + 1; 52 for(int i = 2; i <= 10; ++i) b[i] = b[i - 1] * (inx + 1); 53 ll c[20]; 54 for(int i = 0; i <= 10; ++i){ 55 c[i] = b[i] * a[10 - i]; 56 } 57 58 int cnt = 0; 59 for(int i = 1; i <= 10; ++i){ 60 if(n > c[i - 1] && n <= c[i]) cnt = i; 61 } 62 //----------------------------- 63 64 //if(n == Pow[inx - 1]) cnt = 0; 65 for(int i = 1; i <= inx; ++i) putchar('c'); 66 if(cnt-- > 0) putchar('c'); 67 for(int i = 1; i <= inx; ++i) putchar('o'); 68 if(cnt-- > 0) putchar('o'); 69 for(int i = 1; i <= inx; ++i) putchar('d'); 70 if(cnt-- > 0) putchar('d'); 71 for(int i = 1; i <= inx; ++i) putchar('e'); 72 if(cnt-- > 0) putchar('e'); 73 for(int i = 1; i <= inx; ++i) putchar('f'); 74 if(cnt-- > 0) putchar('f'); 75 for(int i = 1; i <= inx; ++i) putchar('o'); 76 if(cnt-- > 0) putchar('o'); 77 for(int i = 1; i <= inx; ++i) putchar('r'); 78 if(cnt-- > 0) putchar('r'); 79 for(int i = 1; i <= inx; ++i) putchar('c'); 80 if(cnt-- > 0) putchar('c'); 81 for(int i = 1; i <= inx; ++i) putchar('e'); 82 if(cnt-- > 0) putchar('e'); 83 for(int i = 1; i <= inx; ++i) putchar('s'); 84 if(cnt-- > 0) putchar('s'); 85 puts(""); 86 } 87 88 int main() 89 { 90 // ios::sync_with_stdio(false); 91 // cin.tie(0); 92 // cout.tie(0); 93 94 solve(); 95 96 return 0; 97 }