A. Exercising Walk
題目鏈接:點擊這裏
題意:起始點爲 ,需要向左走 步、向右走 步、向下走 步、向上走 步,走路的方向可以按任意順序。問能否在不走出 區域的情況下,走完上述的 步。
思路: 軸 和 軸分開單獨考慮。終點一定是在 ,中間過程儘量往返走(左右抵消、上下抵消),那麼終點一定是能到達的最遠邊界。
特判走了一步就越界的情況(樣例3給出了提示)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int x, y, x1, y1, x2, y2;
scanf("%d%d%d%d%d%d", &x, &y, &x1, &y1, &x2, &y2);
int tx = x - a + b, ty = y - c + d;
if(tx < x1 || tx > x2 || ty < y1 || ty > y2)
{
puts("No");
continue;
}
if(x1 == x2 && a + b != 0)
{
puts("No");
continue;
}
if(y1 == y2 && c + d != 0)
{
puts("No");
continue;
}
puts("Yes");
}
return 0;
}
B. Composite Coloring
題目鏈接:點擊這裏
思路: 個合數, 種顏色足夠用了。注意,不需要我們最小化或最大化顏色的數量,因此,所有最小質因子相同的合數染成同一種顏色即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1010;
int p[N], cnt; // p[]存儲所有素數
bool st[N]; // st[x]存儲x是否被篩掉
int c[N], res[N];
void get_primes()
{
for(int i = 2; i < N; i++)
{
if(st[i]) continue;
p[cnt++] = i;
for(int j = i + i; j < N; j += i)
st[j] = true;
}
}
int main()
{
get_primes();
int T;
scanf("%d", &T);
while(T--)
{
memset(c, 0, sizeof c);
int tot = 0;
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i)
{
int x;
scanf("%d", &x);
for(int j = 0; j < cnt; ++j)
{
if(x % p[j] == 0)
{
if(c[p[j]]) res[i] = c[p[j]]; //該質因子被用過,就和之前的染同一種顏色
else res[i] = c[p[j]] = ++tot; //該質因子沒有被用過,就新開一個顏色
break;
}
}
}
printf("%d\n", tot);
for(int i = 0; i < n; ++i) printf("%d ", res[i]);
puts("");
}
return 0;
}