东华大学2020年程序设计竞赛
A
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 100+10;
const int mod = 1e9+7;
int n;
struct node{
int index;
ll w;
bool operator < (const node e) const{
if(w == e.w) return index < e.index;
else return w > e.w;
}
}e[N];
int main(){
IO;
cin >> n;
int index;
ll a, b, c;
for(int i = 0; i < n; i++){
cin >> index >> a >> b >> c;
e[i].index = index;
e[i].w = a + 2*b + c*3;
}
sort(e, e+n);
cout << e[0].index << " " << e[0].w << "\n";
return 0;
}
B
exgcd求 a x = 1 ( m o d ) p ax=1(mod)p ax=1(mod)p,当 a a a是 p p p的倍数时无解
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
int t;
ll a, p;
ll exgcd(ll a, ll b, ll &x, ll &y){
if(b == 0){
x = 1; y = 0;
return b;
}
ll res = exgcd(b, a%b, x, y);
ll tmp = x;
x = y;
y = tmp - a/b*y;
return res;
}
int main(){
IO;
cin >> t;
while(t--){
cin >> a >> p;
if(a%p == 0){
cout << "-1\n";
}
else{
ll x, y;
exgcd(a, p, x, y);
while(x < 0){
x += p;
y -= a;
}
cout << x%p << "\n";
}
}
return 0;
}
C 最短路
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e6+10;
const int mod = 1e9+7;
int n, m;
struct Edge{
int to, next;
}e[4*N];
int head[N], tot = 0;
void addEdge(int u, int v){
e[tot] = (Edge){v, head[u]};
head[u] = tot++;
}
int d[N], vis[N];
priority_queue< pair<int, int> > q;
void Dij(){
for(int i = 0; i <= n; i++){
d[i] = 1e9; vis[i] = 0;
}
d[1] = 0;
q.push(mk(0, 1));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].to;
if(d[v] > d[u] + 1){
d[v] = d[u] + 1;
q.push(mk(-d[v], v));
}
}
}
}
ll qpow(ll a, int x){
ll res = 1;
while(x){
if(x&1) res = res * a % mod;
a = a * a % mod;
x >>= 1;
}
return res;
}
int main(){
IO;
cin >> n >> m;
memset(head, -1, sizeof(int)*(n+1));
int u, v;
for(int i = 0; i < m; i++){
cin >> u >> v;
addEdge(u, v);
addEdge(v, u);
}
Dij();
ll ans = 0;
for(int i = 2; i <= n; i++){
ans = (ans + qpow(2, d[i])) % mod;
}
cout << ans << "\n";
return 0;
}
D
暴力模拟,每次选最小堆加
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 100+10;
const int mod = 1e9+7;
int t, n, a[N];
int main(){
IO;
cin >> t;
while(t--){
cin >> n;
int sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
sum += a[i];
}
if(sum%n != 0){
cout << "No\n";
continue;
}
sort(a, a+n);
int flag = 1;
for(int i = 1; i < n; i++){
if((a[i]-a[0]) % n != 0){
flag = 0;
}
}
int x = sum/n;
while(flag){
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; i++){
if(a[i] == x) cnt1++;
if(a[i] == 0) cnt2++;
}
if(cnt1 == n) {
flag = 1; break;
}
if(cnt2 > 1){
flag = 0; break;
}
sort(a, a+n);
a[0] += n-1;
for(int i = 1; i < n; i++){
a[i]--;
}
}
if(flag == 0) {
cout << "No\n";
}
else cout << "Yes\n";
}
return 0;
}
F
连续的0变为1,然后1又可以变为0,所以连续的0贡献2步,对结果没有影响。对每个字符串我们只需要统计1的个数就好。
当所有串含1的个数都是偶数时,无论你选择对哪些串进行操作,对方只要也选这些串进行操作,就能继续保证所有串含1的个数都是偶数。由于是Alice先动,所以只有当所有串含1的个数都是偶数时,Alice才会输,其他情况则是Bob落败。
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
int t, n;
string s;
int main(){
IO;
cin >> t;
while(t--){
cin >> n;
int ans = 0;
for(int i = 0; i < n; i++){
cin >> s;
int num = 0;
for(int j = 0; j < s.size(); j++){
if(s[j] =='1') num++;
}
if(num % 2) ans++;
}
if(ans) cout << "sdzNB\n";
else cout << "kgNB\n";
}
return 0;
}
G
最多只有五个数连乘, − 1 ∗ 1 ∗ 1 ∗ 1 ∗ − 1 -1*1*1*1*-1 −1∗1∗1∗1∗−1,然后dp
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false)
#define pb push_back
#define mk make_pair
const int N = 1e5+10;
const int mod = 1e9+7;
int t, n, a[N], dp[N];
int main(){
IO;
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i <= n; i++){
dp[i] = -1e9;
}
for(int i = 1; i <= n; i++){
cin >> a[i];
}
dp[0] = 0;
for(int i = 1; i <= n; i++){
int x = 1;
for(int j = 1; j <= 5; j++){
if(i - j < 0) continue;
x *= a[i-j+1];
dp[i] = max(dp[i], dp[i-j] + x);
}
}
cout << dp[n] << "\n";
}
return 0;
}