AtCoderAGC001
Posted ivorysi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoderAGC001相关的知识,希望对你有一定的参考价值。
AGC001
A - BBQ Easy
从第\(2n - 1\)个隔一个加一下加到1即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
#define MOD 99994711
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int a[205];
void Solve() {
read(N);
for(int i = 1 ; i <= 2 * N ; ++i) read(a[i]);
sort(a + 1,a + 2 * N + 1);
int ans = 0;
for(int i = 2 * N - 1 ; i >= 1 ; i -= 2) {
ans += a[i];
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
B - Mysterious Light
在拐第二次的时候,设\(A = N - x,B = x\)
如果\(B\)小就交换A和B
这个时候相当于用A在B上走,每走A的长度用掉两个A
最后一次回到原点时会少走一个A距离
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
#define MOD 99994711
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int64 N,X;
void Solve() {
read(N);read(X);
int64 ans = N;
int64 A = X,B = N - X;
while(A && B) {
if(B < A) swap(A,B);
ans += (B / A) * A * 2;
int64 t = B % A;
if(t == 0) ans -= A;
B = A;A = t;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
C - Shorten Diameter
枚举中心点使得直径不大于某个偶数
枚举两个相邻点作为奇数直径的两个中心,使得直径不大于某个奇数
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 2005
#define MOD 99994711
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;
}E[MAXN * 2];
int N,sumE,head[MAXN],dep[MAXN],K,fa[MAXN],ans,cnt[MAXN];
vector<pii > Ed;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs(int u) {
cnt[dep[u]]++;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa[u]) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
}
void Solve() {
read(N);read(K);
int a,b;
for(int i = 1 ; i < N ; ++i) {
read(a);read(b);
add(a,b);add(b,a);
Ed.pb(mp(a,b));
}
int ans = N;
for(int i = 1 ; i <= N ; ++i) {
int u = i;
fa[u] = 0;dep[u] = 0;
memset(cnt,0,sizeof(cnt));
dfs(u);
int res = 0;
for(int j = K / 2 + 1 ; j <= N ; ++j) res += cnt[j];
ans = min(ans,res);
}
if(K % 2 == 0) --K;
for(auto t : Ed) {
fa[t.fi] = t.se;fa[t.se] = t.fi;dep[t.fi] = dep[t.se] = 0;
memset(cnt,0,sizeof(cnt));
dfs(t.fi);dfs(t.se);
int res = 0;
for(int i = K / 2 + 1 ; i <= N ; ++i) res += cnt[i];
ans = min(ans,res);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
D - Arrays and Palindrome
硬核构造。。。
奇数段只能放在最前或最后,如果超过两个就不合法
否则构造出第一段-1,第2到M-1段一样,第M段+1的b即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 2005
#define MOD 99994711
#define ba 47
//define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M;
int a[105];
vector<int> v,ans[2];
void Solve() {
read(N);read(M);
for(int i = 1 ; i <= M ; ++i) read(a[i]);
for(int i = 1 ; i <= M ; ++i) {
if(a[i] & 1) v.pb(a[i]);
}
if(v.size() > 2) {puts("Impossible");return;}
if(v.size()) {
int t = v.back();
ans[0].pb(t);
v.pop_back();
}
for(int i = 1 ; i <= M ; ++i) {
if(a[i] % 2 == 0) {
ans[0].pb(a[i]);
}
}
if(v.size()) {
int t = v.back();
ans[0].pb(t);
v.pop_back();
}
for(int i = 0 ; i < ans[0].size() ; ++i) {
if(i == 0) {
if(ans[0][i] != 1) ans[1].pb(ans[0][i] - 1);
}
else if(i == ans[0].size() - 1){
ans[1].pb(ans[0][i] + 1);
}
else ans[1].pb(ans[0][i]);
}
if(ans[0].size() == 1) ans[1].pb(1);
for(auto t : ans[0]) {
out(t);space;
}
enter;
out(ans[1].size());enter;
for(auto t : ans[1]) {
out(t);space;
}
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
E - BBQ Hard
实际上就是匹配两个点就是从\((-A_{i},-B_{i})\)走到\((A_{j},B_{j})\)
我们把所有\((-A_{i},-B_{i})\)设成1,然后dp到所有\((A_{i},A_{i})\)的方案数
最后去重只要减掉自己到自己的方案数再除二即可
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,V = 2000;
int A[MAXN],B[MAXN],dp[4005][4005];
int fac[100005],invfac[100005];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {read(A[i]);read(B[i]);dp[V - A[i]][V - B[i]]++;}
fac[0] = 1;
for(int i = 1 ; i <= 100000 ; ++i) fac[i] = mul(fac[i - 1],i);
invfac[100000] = fpow(fac[100000],MOD - 2);
for(int i = 99999 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
for(int i = 0 ; i <= 2 * V ; ++i) {
for(int j = 0 ; j <= 2 * V ; ++j) {
if(!dp[i][j]) continue;
update(dp[i + 1][j],dp[i][j]);
update(dp[i][j + 1],dp[i][j]);
}
}
int ans = 0;
for(int i = 1 ; i <= N ; ++i) {
update(ans,dp[V + A[i]][V + B[i]]);
update(ans,MOD - C(2 * (A[i] + B[i]),2 * A[i]));
}
ans = mul(ans,(MOD + 1) / 2);
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
以上是关于AtCoderAGC001的主要内容,如果未能解决你的问题,请参考以下文章