【刷题】黄绿题选刷
找到包含节点 1 的环,直接从节点一出发,BFS,如果第二次遍历到了节点1,直接输出时间即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N = 200005;
int n, m;
bool vis[N], f;
int h[N],e[N],ne[N],idx;
void add(int a,int b){
e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}
void bfs(){
queue<PII> q;
q.push({1, 0});
while(!q.empty()){
int u = q.front().fi,ti=q.front().se;
q.pop();
for (int i = h[u]; i;i=ne[i]){
int j = e[i];
if(j==1){
cout << ti + 1 << endl;
f = 1;
return;
}
if(!vis[j]){
q.push({j,ti + 1});
vis[j] = 1;
}
}
}
}
void Main(){
n = rd, m = rd;
FOR(i,1,m){
int a, b;
a = rd, b = rd;
add(a, b);
}
bfs();
if(!f) cout << -1;
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
// T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
其实就是个暴力,用 bitset 优化一下就行。
先写出用 bool 数组能过的代码,再把 bool数组 最后一维用 bitset 优化掉。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N = 2001, M = 1000;
int n, m, a[N][N], res;
bitset<N> cnt[N][M], f;
void Main(){
n = rd, m = rd;
for (int i = 1; i <= n; ++ i )
for (int j = 1; j <= m; ++ j ){
a[i][j] = rd;
cnt[j][a[i][j]][i] = 1;
}
for (int i = 1; i <= n; ++ i ) {
f.reset();
for (int j = 1; j <= m; ++ j ) f ^= cnt[j][a[i][j]];
f[i] = 0;
res += f.count();
}
wt(res/2, '\n');
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
// T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
挺简单的一道题,贪心。
每次肯定是能跑多远跑多远,把 \(2^i\) 打表出来,从大到小枚举,再判断能否整除。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N = 10000005;
ll l,r,h1[N],h2[N],ans,a[N];
void Main(){
l = rd, r = rd;
a[0] = 1;
FOR(i, 1, 62) a[i] = a[i - 1] << 1ll;
while (l != r){
ROF(i,60,0){
if (l + a[i] <= r && l % a[i] == 0){
h1[++ans] = l;
h2[ans] = l + a[i];
l += a[i];
break;
}
}
}
wt(ans, '\n');
FOR(i,1,ans) cout<<h1[i]<<" "<<h2[i]<<endl;
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
// T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
考虑一个图满足
- 不连通
- 每个点和这个点的临点的和为 \(S\)
那么这个图的补图满足
- 联通
- 每个点的所有的邻点的和为 \(T\)(因为顶点总和是不变的,所以 \(T=总顶点和 - S\),而S \(S\) 也是不变的。
构造不连通,且每个点和这个点的临点的和为 \(S\) 的图很容易 构造,按照 \(n\) 的奇偶分类即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
// 输入函数
inline int rd() {
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
void wt(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) wt(x / 10);
putchar(x % 10 + '0');
}
void wt(char x) {
putchar(x);
}
void wt(int x, char k) {
wt(x), putchar(k);
}
namespace Star_F {
void Main() {
int n = rd();
printf("%d\n", n * (n - 1) / 2 - n / 2);
FOR(i, 1, n) {
FOR(j, i + 1, n) {
if (n & 1) {
if (i + j != n) {
printf("%d %d\n", i, j);
}
} else {
if (i + j != n + 1) {
printf("%d %d\n", i, j);
}
}
}
}
}
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ClockA;
int T = 1;
// T = rd();
while (T--) Star_F::Main();
ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
只用操作二,最多进行 \(n-1\) 次即可完成,所一答案最大为 \(n-1\)
考虑操作一怎么使用:
可以考虑反着想,操作二是每次从大到小增加一个数,增加的数肯定是当前序列中的最小值(因为是从大到小添加),然后对这个选的数的位置进行插入排序,看看是否会超过 \(n-1\) 。
具体看代码吧
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], p[N], b[N], ans;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
p[a[i]] = i;
}
ans = n;
int S = 0, m = 0;
for (int i = n; i >= 1; --i) {
b[++m] = p[i];
for (int j = m; j >= 2; --j) {
if (b[j] < b[j - 1]) break;
swap(b[j], b[j - 1]);
++S;
}
if (S > n) break;
ans = min(ans, S + i - 1);
}
cout << ans << '\n';
return 0;
}
输入一棵树,且只有 n−1 条边,所以,肯定无向无环,可以用 DFS 来搜索。
由于限制,所以值为 1 和 n 的点之间应该有一条边。且值为 n 的点只能与 1 相连。
以此类推,尽量将大值放到度为 1的点上.将小值放到与它相邻的点上。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F {
const int N = 1e5 + 5;
vector<int> mp[N];
int n, l, r, num[N];
void dfs(int u, int f) {
for (int v : mp[u]) {
if (v == f) continue;
dfs(v, u);
}
if (!num[u]) {
num[u] = r--;
if (f && !num[f]) num[f] = l++;
}
}
void Main() {
n = rd;
for (int i = 1; i <= n; i++) mp[i].clear(), num[i] = 0;
for (int i = 2; i <= n; i++) {
int u = rd, v = rd;
mp[u].emplace_back(v);
mp[v].emplace_back(u);
}
l = 1, r = n;
dfs(1, 0);
for (int i = 1; i <= n; i++)
wt(num[i], ' ');
wt('\n');
}
}
signed main() {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
打表题,可以爆搜找通项公式
当然也可以:
题解 ----- cff_0102
挺简单的,先排个序,然后肯定尽可能的让序列变成 \(1,2,3,...n-1,n\) ,如果某个 \(a_i < i\) ,那就不管他,加入备选集合。否则加入必选集合
如果 \(必选集合总和 <= 备选集合元素数量+1 && 必选集合总和 + 备选集合总和 >= 备选集合元素数量+1\) 那么答案加一(通俗的说就是通过删除的数能凑出来一个更大的)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
#define ll long long
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#define int long long
inline int rd() {
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x) {
putchar(x);
}
void wt(int x, char k) {
wt(x), putchar(k);
}
namespace Star_F {
const int N = 100005;
int a[N];
void Main() {
int n = rd, A = 0, B = 0, f = 1;
FOR(i,1,n) a[i] = rd;
sort(a + 1, a + 1 + n);
FOR(i,1,n)
if (a[i] >= f) A += a[i] - (f++);
else B += a[i] - 1;
wt(f+(A <= f && A + B >= f),'\n');
}
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ClockA;
int T = 1;
T = rd;
while (T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
*/
贪心的想,肯定要把小的数两两消除。
枚举消除的长度,两种零食消除的长度可能相差一,再分别计算即可。
(维护一个后缀和,直接 \(\mathcal{O}(1)\) 计算即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << '=' << x << endl
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N=100005;
int n, m, a[N], b[N],A[N], B[N];
int f__a(int x){
if(x<=n) return A[x];
return 0;
}
int f__b(int x){
if(x<=m) return B[x];
return 0;
}
void Main(){
n = rd;
FOR(i,1,n) a[i] = rd;
m = rd;
FOR(i,1,m) b[i] = rd;
sort(a + 1, a + n + 1);
sort(b + 1, b + m + 1);
A[n + 1] = B[m + 1] = 0;
ROF(i,n,1)
A[i] = A[i + 1] + a[i];
ROF(i,m,1)
B[i] = B[i + 1] + b[i];
int ans = -0x7fffffffffffffff;
FOR(i,1,min(n,m)){
if (i <= n && i <= m)
ans = max(f__a(i + 1) + f__b(i + 1), ans);
if (i + 1 <= n && i <= m)
ans = max(f__a(i + 2) + f__b(i + 1), ans);
if (i <= n && i + 1 <= m)
ans = max(f__a(i + 1) + f__b(i + 2), ans);
}
wt(ans, '\n');
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
看到 异或 操作考虑字典树。
遍历 \(k\) 的每一位,在分讨 \(a,b\) 这位的取值,什么时候能产生贡献,分别计算即可。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << " = " << x << endl
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
//#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N = 1e6 + 5;
int n, q, a[N], b[N];
struct Tree{
int tr[N * 30][2], cnt[N * 30][4], ed[N * 30], cc;
void insert(int x,int y){
int u = 0;
for (int i = 30; i >= 0;i--){
int c = x >> i & 1, d = y >> i & 1;
cnt[u][c * 2 + d]++;
if(!tr[u][c^d])
tr[u][c ^ d] = ++cc;
u = tr[u][c ^ d];
}
ed[u]++;
}
int query(int x){
int u = 0, ans = 0;
bool f = 1;
for (int i = 30; i >= 0;i--){
int c = x >> i & 1;
ans += ed[u] + cnt[u][c << 1 | 1];
if(tr[u][c])
u = tr[u][c];
else{
f = 0;
break;
}
}
if(f)
ans += ed[u];
return ans;
}
} T;
void Main(){
int t;
t = rd, n = rd, q = rd;
FOR(i, 1, n) a[i] = rd;
FOR(i, 1, n) b[i] = rd;
FOR(i, 1, n) T.insert(a[i], b[i]);
int k = 0, lst = 0;
while(q--){
k = rd;
k ^= lst * t;
wt(lst=T.query(k), '\n');
}
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
// T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
分析公式题。
看到有大量重复且复杂的式子,考虑换元。
一大堆不等式条件,考虑转化为最关键元素的上下界。
本题就是缩小上下界,然后维护一个后缀最小值进行判断。
感觉挺水的。
设 \(d = m/n\),然后 \(n\) 每次乘以 \(gcd(n,d)\) ,相应的,\(n*=gcd(n,d),d/=gcd(n,d)\)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << " = " << x << endl
typedef pair <int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x),putchar(k);
}
namespace Star_F{
const int N = 300005;
void Main(){
int n = rd, m = rd;
if(m%n)
wt(-1, '\n');
else{
int d = m / n, ans = 0;
while(1){
int g = __gcd(n, d);
if(g==1)
break;
d /= g, n *= g, ans++;
}
if(d>1)
wt(-1, '\n');
else
wt(ans, '\n');
}
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T=1;
T=rd;
while(T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
DFS,没啥好说的,难点在代码。
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << " = " << x << endl
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
// #define int long long
inline int rd() {
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x) {
putchar(x);
}
void wt(int x, char k) {
wt(x), putchar(k);
}
namespace Star_F {
const int M = 3005,N=100005;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int a[M][M], n, m, q, r, k, ans;
int t[M][M];
int sx[N], sy[N], snum;
bool f(int x, int y) {
FOR(i, 0, 3) {
int nx = x + dx[i], ny = y + dy[i];
if (a[nx][ny] == 1) return 1;
}
return 0;
}
void dfs(int x, int y) {
FOR(i, 0, 3) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m)
if (f(nx, ny) && !a[nx][ny]) {
a[nx][ny] = 2;
t[nx][ny] = t[x][y] + 1;
dfs(nx, ny);
}
}
}
void Main() {
n = rd, m = rd, q = rd, r = rd, k = rd;
FOR(i, 1, q) {
int lx, ly, rx, ry;
lx = rd, ly = rd, rx = rd, ry = rd;
FOR(col, lx, rx)
FOR(row, ly, ry)
a[col][row] = 1;
}
FOR(i, 1, r) {
int ti, x, y;
ti = rd, x = rd, y = rd;
t[x][y] = ti;
a[x][y] = 2;
if (!f(x, y))
sx[++snum] = x, sy[snum] = y;
dfs(x, y);
}
FOR(j, 1, snum) {
int x = sx[j], y = sy[j];
bool flag = 0;
FOR(i, 0, 3) {
int nx = x + dx[i], ny = y + dy[i];
if (a[nx][ny] == 2 && t[nx][ny] - t[x][y] <= k) {
flag = 1;
break;
}
}
if (!flag) a[x][y] = 0;
}
FOR(i, 1, n)
FOR(j, 1, m)
if (a[i][j] == 2) ans++;
wt(ans, '\n');
}
}
signed main() {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T = 1;
// T = rd;
while (T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
首先观察到答案不超过2。
判断答案为0直接模拟一变过程即可。
难点在于如何判断答案为 1:
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << " = " << x << endl
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x), putchar(k);
}
namespace Star_F {
const long long N = 2e5 + 10;
int q, n, a[N];
string s1, s2;
map<char, int> pa;
void Main() {
pa['P'] = 1;
pa['W'] = 2;
pa['V'] = 3;
cin >> q;
while (q--) {
cin >> n >> s1 >> s2;
n <<= 1;
FOR(i, 1, n) {
if (i % 2) a[i] = pa[s1[i >> 1]];
else a[i] = pa[s2[(i >> 1) - 1]];
}
int cnt1 = 0, cnt2 = 0, cnt3 = 0, p = N;
bool f = 0;
FOR(i, 1, n) {
if (a[i] == 1) cnt1++;
else if (a[i] == 2) cnt2++;
else cnt3++;
if (cnt1 == cnt2 && cnt2 == cnt3) {
if (i % 2) {
wt(0, '\n');
f = 1;
}
p = i;
break;
}
}
if (f) continue;
cnt1 = cnt2 = cnt3 = 0;
int t12 = 0, t13 = 0, t21 = 0, t23 = 0, t31 = 0, t32 = 0;
for (int i = 1; i <= n; i += 2) {
if (a[i] == 1) cnt1++;
else if (a[i] == 2) cnt2++;
else cnt3++;
if (cnt1 - 1 == cnt2 && cnt2 - 1 == cnt3 && p > t12) {
wt(1, '\n');
f = 1;
break;
} else if (cnt1 - 1 == cnt3 && cnt3 - 1 == cnt2 && p > t13) {
wt(1, '\n');
f = 1;
break;
} else if (cnt2 - 1 == cnt1 && cnt1 - 1 == cnt3 && p > t21) {
wt(1, '\n');
f = 1;
break;
} else if (cnt2 - 1 == cnt3 && cnt3 - 1 == cnt1 && p > t23) {
wt(1, '\n');
f = 1;
break;
} else if (cnt3 - 1 == cnt1 && cnt1 - 1 == cnt2 && p > t31) {
wt(1, '\n');
f = 1;
break;
} else if (cnt3 - 1 == cnt2 && cnt2 - 1 == cnt1 && p > t32) {
wt(1, '\n');
f = 1;
break;
}
if (a[i + 1] == 1) cnt1++;
else if (a[i + 1] == 2) cnt2++;
else cnt3++;
if (cnt1 - 1 == cnt2 && cnt2 - 1 == cnt3) t12 = i;
else if (cnt1 - 1 == cnt3 && cnt3 - 1 == cnt2) t13 = i;
else if (cnt2 - 1 == cnt1 && cnt1 - 1 == cnt3) t21 = i;
else if (cnt2 - 1 == cnt3 && cnt3 - 1 == cnt1) t23 = i;
else if (cnt3 - 1 == cnt1 && cnt1 - 1 == cnt2) t31 = i;
else if (cnt3 - 1 == cnt2 && cnt2 - 1 == cnt1) t32 = i;
}
if (!f) wt(2, '\n');
}
}
}
signed main() {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T = 1;
// T = rd;
while (T--) Star_F::Main();
// ClockB;
return 0;
}
/*
* ▀▀▀██████▄▄▄ _______________
* ▄▄▄▄▄ █████████▄ / \
* ▀▀▀▀█████▌ ▀▐▄ ▀▐█ | Code has no BUG! |
* ▀▀█████▄▄ ▀██████▄██ | _________________/
* ▀▄▄▄▄▄ ▀▀█▄▀█════█▀ |/
* ▀▀▀▄ ▀▀███ ▀ ▄▄
* ▄███▀▀██▄████████▄ ▄▀▀▀▀▀▀█▌ ______________________________
* ██▀▄▄▄██▀▄███▀ ▀▀████ ▄██ █ \\
* ▄▀▀▀▄██▄▀▀▌████▒▒▒▒▒▒███ ▌▄▄▀▀▀▀█_____________________________ //
* ▌ ▐▀████▐███▒▒▒▒▒▐██▌
* ▀▄▄▄▄▀ ▀▀████▒▒▒▒▄██▀
* ▀▀█████████▀
* ▄▄██▀██████▀█
* ▄██▀ ▀▀▀ █
* ▄█ ▐▌
* ▄▄▄▄█▌ ▀█▄▄▄▄▀▀▄
* ▌ ▐ ▀▀▄▄▄▀
* ▀▀▄▄▀ ██
* \ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
* \- ▌ Name: Star_F ▀ ▀
* - ▌ (o) ▀
* /- ▌ Go Go Go ! ▀ ▀
* / ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ ▀
*/
很奇怪的构造法
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define DEBUG(x) cerr << #x << " = " << x << endl
typedef pair<int, int> PII;
typedef unsigned int uint;
typedef unsigned long long ull;
#define i128 __int128
#define fi first
#define se second
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
#define ClockA clock_t start, end; start = clock()
#define ClockB end = clock(); cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
//#define int long long
inline int rd(){
int 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 << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
#define rd rd()
void wt(int x){
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
wt(x / 10);
putchar(x % 10 + '0');
return;
}
void wt(char x){
putchar(x);
}
void wt(int x, char k){
wt(x), putchar(k);
}
namespace Star_F {
int a, b, c, d;
void solve() {
int cnt = 0, nc = a;
while (nc % 2 == 0) nc /= 2, cnt++;
c = pow(2, cnt + 1), d = a + c;
cout << 1 << " " << c << " " << d << '\n';
}
void Main() {
int T = rd;
while (T--) {
a = rd;
solve();
}
}
}
signed main() {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ClockA;
int T = 1;
// T = rd;
while (T--) Star_F::Main();
// ClockB;
return 0;
}
发现只要确定了 \(p1\),其他的都可以地推出来。
这样做时间复杂度是 \(\mathcal{O}(n^2)\)
可以发现 \(p[i]\) 是大于 \(0\) 的,所以 ,\(sum[i]^p[1] != 0\),也就是说 \(p[1]!=sum[i]\),($sum[i] 表示前 \(i\) 个 \(b[i]\) 异或起来。
所以可以进行剪枝,时间复杂度 \(\mathcal{O}(n)\)