Codeforces Round #671 (Div. 2)
比赛传送门
A - Digit Game
Code
def main():
t = eval(input())
for cas in range(t):
n = eval(input())
s = input()
lens = len(s)
odd, even = 0, 0
for i in range(lens):
tmp = int(s[i])
if i % 2 == 1 and tmp % 2 == 0:
even += 1
elif i % 2 == 0 and tmp % 2 == 1:
odd += 1
if n % 2 == 0:
if even != 0:
print(2)
else:
print(1)
else:
if odd != 0:
print(1)
else:
print(2)
main()
B - Stairs
Code
pre = []
def init():
global pre
pre.append(0)
now = 1
tmp = 0
for i in range(1, 50):
tmp = tmp * 2 + now * now
pre.append(pre[-1] + tmp)
now = now * 2
def check(tmp, x):
global pre
return pre[tmp] <= x
def main():
global pre
init()
# for i in range(50):
# print(pre[i])
t = eval(input())
for cas in range(t):
x = eval(input())
l, r = 1, len(pre)-1
ans = 1
while l <= r:
mid = (l+r)//2
if check(mid, x):
l = mid + 1
ans = mid
else:
r = mid - 1
print(ans)
main()
C - Killjoy
Code
def main():
t = eval(input())
for cas in range(t):
n, x = map(int, input().split())
arr = list(map(int, input().split()))
flag = True
flagg = False
tol = 0
for i in arr:
if i != x:
flag = False
else:
flagg = True
tol += i
if flag:
print(0)
else:
if tol == x * n:
print(1)
elif flagg:
print(1)
else:
print(2)
main()
1419D1 - Sage’s Birthday (easy version)
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
#define make_pair() mkp(a,b)
const int N = 1e5+5;
const int M = 20 + 5;
const ll MOD = 1e9 + 7;
int arr[N];
int ans[N];
int main()
{
int n;
scanf("%d", &n);
rep(i,1,n) scanf("%d", &arr[i]);
sort(arr+1, arr+1+n);
printf("%d\n", (n-1)/2);
rep(i,1,n/2)
ans[i*2] = arr[i];
rep(i,n/2+1,n)
ans[(i-(n/2))*2-1] = arr[i];
rep(i,1,n)
printf("%d ", ans[i]);
printf("\n");
return 0;
}
D2 - Sage’s Birthday (hard version)
Code
吐槽:这easy和hard构造方式就没区别啊,hard就重新算一下个数。。。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
#define make_pair() mkp(a,b)
const int N = 1e5+5;
const int M = 20 + 5;
const ll MOD = 1e9 + 7;
int arr[N];
int ans[N];
int main()
{
int n;
scanf("%d", &n);
rep(i,1,n) scanf("%d", &arr[i]);
sort(arr+1, arr+1+n);
rep(i,1,n/2)
ans[i*2] = arr[i];
rep(i,n/2+1,n)
ans[(i-(n/2))*2-1] = arr[i];
int tol = 0;
rep(i,2,n-1)
if(ans[i-1] > ans[i] && ans[i+1] > ans[i])
tol ++;
printf("%d\n", tol);
rep(i,1,n)
printf("%d ", ans[i]);
printf("\n");
return 0;
}
E - Decryption
Code
#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
#define rep(i,a,b) for(int i = (a); i <= (b); i ++)
#define per(i,a,b) for(int i = (a); i >= (b); i --)
#define mkp(a,b) make_pair(a,b)
//#define __int128 ll
const int N = 2e5 + 5;
//const int M = 25 + 5;
const int INF = 0x3f3f3f3f;
//const double EPS = 1e-9;
//const double PI = acos(-1);
//const double PI = 3.14;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
//const int dx[] = {-1, 1, 0, 0};
//const int dy[] = {0, 0, -1, 1};
int prime[N+5];
VI G_yin, G_pri;
map<int, int> M;
void getPrime()
{
memset(prime,0,sizeof(prime));
for(int i=2; i<=N; i++)
{
if(!prime[i])
prime[++prime[0]]=i;
for(int j=1; j<=prime[0]&&prime[j]<=N/i; j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)
break;
}
}
}
void init()
{
G_yin.clear();
G_pri.clear();
M.clear();
}
int main()
{
getPrime();
int T;
scanf("%d", &T);
while(T --)
{
init();
int n;
scanf("%d", &n);
int sqr = sqrt(n);
rep(i,2,sqr)
{
if(n%i == 0)
G_yin.push_back(i);
if(n%(n/i) == 0 && n/i != i)
G_yin.push_back(n/i);
}
G_yin.push_back(n);
sort(G_yin.begin(), G_yin.end());
int tmp = n;
rep(i,2,sqr)
{
if(tmp%i == 0)
G_pri.push_back(i);
while(tmp%i == 0)
tmp /= i;
}
if(tmp>1)
G_pri.push_back(tmp);
sort(G_pri.begin(), G_pri.end());
int sz_yin = G_yin.size();
int sz_pri = G_pri.size();
if(sz_pri == 1)
{
rep(i,0,sz_yin-1)
printf("%d ", G_yin[i]);
printf("\n");
printf("0\n");
}
else if(sz_pri==2 && sz_yin==3)
{
rep(i,0,sz_yin-1)
printf("%d ", G_yin[i]);
printf("\n");
printf("1\n");
}
else
{
VI Now;
rep(i,0,sz_pri-1)
{
int d1 = G_pri[i], d2 = G_pri[(i+1)%sz_pri];
rep(j,0,sz_yin-1)
{
if(M[G_yin[j]]==0 && G_yin[j]%d1==0 && G_yin[j]%d2==0)
{
Now.push_back(G_yin[j]);
M[G_yin[j]] = 1;
break;
}
}
}
// cout << Now.size() << endl;
rep(i,0,sz_pri-1)
{
rep(j,0,sz_yin-1)
{
if(M[G_yin[j]]==0 && G_yin[j]%G_pri[i]==0) {
printf("%d ",G_yin[j]);
M[G_yin[j]] = 1;
}
}
printf("%d ", Now[i]);
}
printf("\n");
printf("0\n");
}
}
return 0;
}