A. Dice Rolling
把(x)分解为(a * 6 + b),其中(a)是满6数,(b)满足(1 <= b < 6),即可...
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int T; scanf("%d", &T);
while(T--){
int x; scanf("%d", &x);
printf("%d
", x % 6 ? x / 6 + 1 : x / 6);
}
return 0;
}
B. Letters Rearranging
判断,如果回文就直接调整一位即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
char s[N];
int cnt[26], tot = 0, loc = 0;
int main(){
int T; scanf("%d", &T);
while(T--){
memset(cnt, 0, sizeof cnt); loc = tot = 0;
bool ep = false;
scanf("%s", s + 1);
int n = strlen(s + 1);
for(int i = 1; i <= n; i++){
cnt[s[i] - 'a'] ++;
}
for(int i = 0; i < 26; i++){
if(cnt[i]) tot++, loc = i;
}
if(tot == 1) puts("-1");
else{
for(int i = 1; i <= n; i++)
if(s[i] == s[n - i + 1] && i != n - i + 1){
for(int j = 1; j <= n; j++){
if(i != j && j != n - i + 1){
if(s[j] != s[i]){
swap(s[j], s[i]);
printf("%s
", s + 1);
ep = true; break;
}
}
}
if(ep) break;
}
if(!ep) printf("%s
", s + 1);
}
}
return 0;
}
C. Mishka and the Last Exam
贪心构造,尽量使每个(a[i] (1 <= i <= n / 2))最小:
在符合(a[i - 1] <= a[i])的条件下,也要满足(a[n - i + 1] <= a[n - i + 1 + 1]),所以说这个界限为:
(a[i] = max(a[i - 1], b[i] - a[n - i + 1 + 1])) 注意边界,把(a[n + 1]) 赋值为极大值。
#include <iostream>
#include <cstdio>
#include <limits.h>
using namespace std;
typedef long long LL;
const int N = 200010;
int n;
LL a[N], b[N >> 1];
int main(){
scanf("%d", &n);
a[n + 1] = 9223372036854775807ll;
for(int i = 1; i <= (n >> 1); i++) {
scanf("%lld", b + i);
a[i] = max(a[i - 1], b[i] - a[n - i + 1 + 1]);
a[n - i + 1] = b[i] - a[i];
}
for(int i = 1; i <= n; i++)
printf("%lld ", a[i]);
return 0;
}
D. Beautiful Graph
实质是一个二分图染色问题,对于选定每个奇数后,偶数是对应主线的,所以只需算奇数的方案即可。
对于每一个联通快而言相互没有影响,它们的方案是:
(2 ^ {cnt0} + 2 ^ {cnt1})其中两个(cnt)代表二分图染色两个色彩的数量,颜色可以调换,且填奇数,每个位置有两种选择...
答案就是每个联通快的相乘,注意如果二分图失败了就(ans = 0)吧
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 300010, M = 300010, MOD = 998244353;
int n, m, numE, head[N], f[N], ans;
struct Edge{
int next, to;
}e[M << 1];
void addEdge(int from, int to){
e[++numE].next = head[from];
e[numE].to = to;
head[from] = numE;
}
queue<int> q;
int power(int a, int b){
int res = 1;
while(b){
if(b & 1) res = (LL)res * a % MOD;
a = (LL)a * a % MOD;
b >>= 1;
}
return res;
}
int main(){
int T; scanf("%d", &T);
while(T--){
while(q.size()) q.pop();
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) head[i] = 0, f[i] = -1;
numE = 0; ans = 1;
for(int i = 1; i <= m; i++){
int u, v; scanf("%d%d", &u, &v);
addEdge(u, v); addEdge(v, u);
}
for(int i = 1; i <= n; i++){
if(~f[i]) continue;
int cnt0 = 1, cnt1 = 0;
f[i] = 0; q.push(i);
while(!q.empty()){
int u = q.front(); q.pop();
for(int i = head[u]; i; i = e[i].next){
int v = e[i].to;
if(f[v] == -1){
f[v] = f[u] ^ 1;
if(!f[v]) cnt0++;
else cnt1++;
q.push(v);
}else if(f[v] != (f[u] ^ 1)){
ans = 0; break;
}
}
if(!ans) break;
}
if(!ans) break;
ans = (ans * ((LL)(power(2, cnt0) + power(2, cnt1)) % MOD)) % MOD;
}
printf("%d
", ans);
}
return 0;
}