codeforces1328B

个人博客链接:https://blog.nuoyanli.com/2020/03/27/cf1328b/

题目链接

http://codeforces.com/contest/1328/problem/B

题意

t t 组数据,每次给你两个数 n k ( 3 n 1 0 5 , 1 k m i n ( 2 1 0 9 , n ( n 1 ) 2 ) ) n,k(3 \leq n \leq 10^5,1\leq k \leq min(2*10^9,\frac{n⋅(n−1)}{2})) ,问你由 n 2 n-2 a a 2 2 b b 组成的字符串的第 k k 小的字典序的字符串是什么。

在这里插入图片描述

思路

我们通过观察可以知道规律,1+2+3+…+n,符合前 n n 项和的部分规律,第 k k 个串由两个 b b 的位置唯一确定,由后往前找第一个 b b 的位置,这个位置就是前 n n 项和大于 k k 的位置,第二个位置就是前 n 1 n-1 项和与 k k 的差,详细见代码。

参考代码

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define PB push_back
#define FI first
#define SE second
#define m_p(a, b) make_pair(a, b)
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double f = 2.32349;
void solve() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    LL n, k;
    cin >> n >> k;
    int x = 0, y = 0;
    while (1) {
      y++;
      x = y * (y + 1) / 2;
      if (x >= k) {
        break;
      }
    }
    x = y * (y - 1) / 2;
    x = k - x;
    cout << x << " " << y << endl;
    y = n - y;
    x = n + 1 - x;
    cout << x << " " << y << endl;
    for (int i = 1; i <= n; i++) {
      if (i == x || i == y) {
        cout << "b";
      } else {
        cout << "a";
      }
    }
    cout << endl;
  }
}
signed main() {
  solve();
  return 0;
}
发布了322 篇原创文章 · 获赞 247 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/nuoyanli/article/details/105141208