Codeforces Round #609 (Div. 2)
题库链接
https://codeforces.com/contest/1269
A. Equation
给定一个数n,求两个合数a,b,使得a-b=n
如果n是偶数,显然找两个偶数就好了,如果n是奇数,找一个奇数,再找一个偶数
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200000;
const int M = 1e9+7;
int n,m,k,t;
bool notprime(int x)
{
for(int i = 2; i<= sqrt(x); i++)
{
if(x%i == 0) return true;
}
return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin>>n;
if(n%2)
{
for(int i = 4; i < maxn; i += 2)
{
if(notprime(i+n))
{
cout<<i+n<<' '<<i<<endl;
return 0;
}
}
}
else cout<<n+10<<' '<<10<<endl;
return 0;
}
B. Modulo Equality
2个数组,长度是n,元素上限是m,找一个最小的x,使得\((a_i + x) \bmod m = b_{p_i}\)
暴力出所有的可能的x,\(x = (b_0 - a_i + m) \bmod m\),然后判断a是否可以变成b
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 2100;
const int M = 1e9+7;
int n,m,k,t;
int a[maxn];
int b[maxn];
int c[maxn];
bool judge(int x)
{
for(int i = 0; i < n; i++)
{
c[i] = (a[i]+x)%m;
}
sort(c,c+n);
for(int i = 0; i < n; i++)
{
if(c[i] != b[i]) return false;
}
return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin(n),cin(m);
for(int i = 0; i < n; i++)
{
cin(a[i]);
}
for(int i = 0; i < n; i++)
{
cin(b[i]);
}
sort(b,b+n);
set<int> st;
for(int i = 0; i < n; i++)
{
st.insert((b[0]-a[i]+m)%m);
}
for(auto i : st)
{
if(judge(i))
{
cout<<i<<endl;
return 0;
}
}
return 0;
}
C. Long Beautiful Integer
给定一个数a,a的长度n,循环节长度k,求一个最小的b,满足\(b_i = b_{i+k}\),并且b>a
首先判断最小的b满不满足大于a,如果不满足就b[k-1]+1,然后判断一下进位就好了,交的时候把'0'写成了'9',也是醉了
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 200100;
const int M = 1e9+7;
int n,m,k,t;
char s[maxn];
char ss[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin(n),cin(k);
cin>>s;
for(int i = 0; i < n; i++)
{
ss[i] = s[i%k];
}
if(strcmp(ss,s) >= 0) {
cout<<strlen(ss)<<endl;
cout<<ss<<endl;
}
else
{
s[k-1]++;
int a = k-1;
while (s[a] == '9'+1)
{
s[a] = '0'; //不小心写错了,自闭了
s[a-1]++;
a--;
}
for(int i = 0; i < n; i++)
{
ss[i] = s[i%k];
}
cout<<strlen(ss)<<endl;
cout<<ss<<endl;
}
return 0;
}
D. Domino for Young
一个图表,第i列长度为\(a_i\),\(a_i \geq a_{i+1}\),求最多可以放下多少(2,1),或者(1,2)的多米诺骨牌
官方的题解,把这些格子染色,白色边上是黑色,黑色边上是白色,然后ans = min(白,黑)
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define pii pair<int,int>
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 300100;
const int M = 1e9+7;
int n,m,k,t;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif
cin>>n;
ll b = 0,w = 0;
for(int i = 1,x; i <= n; i++)
{
cin>>x;
if(i%2)
{
b += (x+1)/2;
w += (x)/2;
}
else
{
w += (x+1)/2;
b += (x)/2;
}
}
cout<<min(b,w)<<endl;
return 0;
}