codeforces1328B

個人博客鏈接:https://blog.nuoyanli.com/2020/03/27/cf1328b/

題目鏈接

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

題意

tt組數據,每次給你兩個數nk(3n105,1kmin(2109,n(n1)2))n,k(3 \leq n \leq 10^5,1\leq k \leq min(2*10^9,\frac{n⋅(n−1)}{2})),問你由n2n-2aa22bb組成的字符串的第kk小的字典序的字符串是什麼。

在這裏插入圖片描述

思路

我們通過觀察可以知道規律,1+2+3+…+n,符合前nn項和的部分規律,第kk個串由兩個bb的位置唯一確定,由後往前找第一個bb的位置,這個位置就是前nn項和大於kk的位置,第二個位置就是前n1n-1項和與kk的差,詳細見代碼。

參考代碼

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章