Codeforces - 1166D - Cute Sequences
地址
http://codeforces.com/contest/1166/problem/D
原文地址
https://www.lucien.ink/archives/433
題目
Given a positive integer , we say that a sequence of positive integers is -cute if for every index such that it holds that for some positive integer satisfying .
You will be given queries consisting of three positive integers , and . For each query you must determine whether or not there exists an -cute sequence whose first term is and whose last term is . If such a sequence exists, you must additionally find an example of it.
題意
定義 , ,給你 a b m
,判斷是否存在一個一個序列 ,假定這個序列的長度爲 ,使得 且 。
題解
規定 ,歸納一下,可以發現:
即判斷是否存在一個 滿足 式且 。
可以進一步簡寫一下,如果將 代入 式,且令: 則有: 可以發現忽略 式中的 之後,剩下的式子爲 爲一個二進制表示,我們可以將 整體減去一個 從而消掉 ,這樣一來可以表示爲 ,令 的二進制表示爲 ,此時 , 。
然後暴力輸出 序列即可。
代碼
#include <bits/stdc++.h>
const int maxn = 54;
typedef long long ll;
ll __pow[maxn] = {1}, r[maxn], a, b, m;
int solve() {
if (a == b) return 1;
for (int n = 2; __pow[n - 2] <= b; n++) {
ll div = b / __pow[n - 2], mod = b % __pow[n - 2];
if (div > a && (div - a < m || (mod == 0 && div - a <= m)) && __pow[n - 2] - 1 >= mod) {
for (int i = 2, bit = n - 3; bit >= 0; i++, bit--) r[i] = (mod >> bit & 1) + div - a;
return n;
}
}
return -1;
}
ll calc(int x, int n) {
if (x > 1 && x < n) {
ll ret = __pow[x - 2] * a;
for (int i = 2; i < x; i++) ret = ret + __pow[x - i - 1] * r[i];
return ret + r[x];
} else return x == 1 ? a : b;
}
int main() {
int q, n;
for (int i = 1; i < maxn; i++) __pow[i] = __pow[i - 1] << 1;
for (scanf("%d", &q); q; q--) {
scanf("%lld%lld%lld", &a, &b, &m);
if (~(n = solve())) {
printf("%d ", n);
for (int i = 1; i <= n; i++) printf("%lld%c", calc(i, n), i == n ? '\n' : ' ');
} else puts("-1");
}
return 0;
}