A:Forbidden Integer
题意:给你整数n ,k , x ,用1 ~ k 除了x以外的数去表示n(数的和等于n )
题解 :明显可知,当x不等于1时,n能用n个1去表示 , 当x等于1时,明显当k也等于1时,无解,当k等于2时,n只有偶数才能被表示;当k大于2时,由数论知识得知,任何数都能被2 ,3叠加表示;
AC代码
#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
void solved()
{
int n,k,x;
cin >> n >> k >> x;
if (x != 1)
{
cout << "YES" << endl;
cout << n << endl;
for (int i = 1; i <= n; i++) cout << 1 << " ";
cout << endl;
}
else
{
if (k == 1 || ((n & 1) && k < 3)) cout << "NO" << endl;
else
{
cout << "YES" << endl;
if (n % 2 == 0)
{
cout << n / 2 << endl;
while (n)
{
cout << 2 << " ";
n -= 2;
}
cout << endl;
}
else
{
cout << 1 + (n - 3) / 2 << endl;
cout << 3 << " ";
n -= 3;
while (n)
{
cout << 2 << " ";
n -= 2;
}
cout << endl;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t -- )
solved();
return 0;
}
B. Come Together
题意:给你A, B, C的坐标,去计算A-C和A-B的最短路径重合的路径长度
题解:只需判断B与C相对A的位置,如果BC都在A的同一方(左或右),当同时在上下方的一方时那么答案为min(abs(x[0] - x[1]) , abs(x[0] - x[2])) + min(abs(y[0] - y[1]) , abs(y[0] - y[2]));,否则min(abs(x[0] - x[1]) , abs(x[0] - x[2]));,如果BC不在同一左右方时 ,如果同时在上下方的一方则答案为min(abs(y[0] - y[1]) , abs(y[0] - y[2]));,否则没有,因为起点算一个,所以答案为上述在+1
AC代码
#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
void solved()
{
LL x[3] , y[3];
for(int i = 0 ; i < 3 ; i ++ ) cin >> x[i] >> y[i];
LL sum = (x[0] - x[1]) * (x[0] - x[2]);
LL cnt = (y[0] - y[1]) * (y[0] - y[2]);
LL ans = 1;
if(sum >= 0)
{
if(cnt >= 0) ans += min(abs(x[0] - x[1]) , abs(x[0] - x[2])) + min(abs(y[0] - y[1]) , abs(y[0] - y[2]));
else ans += min(abs(x[0] - x[1]) , abs(x[0] - x[2]));
}else
{
if(cnt >= 0) ans += min(abs(y[0] - y[1]) , abs(y[0] - y[2]));
else ans = 1;
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t -- )
solved();
return 0;
}