A
大意就是判断会不会在一个平年中出现相邻的两个月的第一天分别是给定的日期。
判断一下
31
mod
7
,
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
char ch[100];
int x, y;
int main(void) {
freopen("1.in", "r", stdin);
gets(ch);
if (ch[0] == 'm') x = 1;
else if (ch[0] == 't' && ch[1] == 'u') x = 2;
else if (ch[0] == 'w') x = 3;
else if (ch[0] == 't') x = 4;
else if (ch[0] == 'f') x = 5;
else if (ch[0] == 's' && ch[1] == 'a') x = 6;
else x = 7;
gets(ch);
if (ch[0] == 'm') y = 1;
else if (ch[0] == 't' && ch[1] == 'u') y = 2;
else if (ch[0] == 'w') y = 3;
else if (ch[0] == 't') y = 4;
else if (ch[0] == 'f') y = 5;
else if (ch[0] == 's' && ch[1] == 'a') y = 6;
else y = 7;
y -= x; y = (y + 7) % 7;
if (y == 0 || y == 2 || y == 3) puts("YES");
else puts("NO");
}
B
You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from
0 to n+1 actions in total. Operations can be performed in any order.既然最后可以交换两列,那么对于一个排序号的排列枚举交换的位置,一一匹配即可
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int mp[30][30], a[30][30], b[30]; int n, m, cnt; bool flag = 0; int main(void) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &mp[i][j]); for (int i = 1; i <= m; i++) b[i] = i; for (int i = 1; i <= n; i++) { cnt = 0; for (int j = 1; j <= m; j++) cnt += (mp[i][j] != b[j]); if (cnt > 2) goto w; } flag = 1; w:; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) { swap(b[i], b[j]); for (int k = 1; k <= n; k++) { cnt = 0; for (int x = 1; x <= m; x++) cnt += (mp[k][x] != b[x]); if (cnt > 2) goto W; } flag = 1; W:swap(b[i], b[j]); } if (flag) puts("YES"); else puts("NO"); return 0; }
C
给定 n∗m 的网格,从 (0,0) 有一道射线以 v=2√ 射出,遇到墙壁反射遵守反射定律。求射线经过 (x,y) 的时间。可以把这张网格图拓展成 (lcm(n,m),lcm(n,m)) ,然后发现其实总共四种情况 (x,y) 、 (2∗n−x,y) 、 (x,2∗m−y) 、 (2∗n−x,2∗m−y)
只要预处理出每过 m <script type="math/tex" id="MathJax-Element-21">m</script>时间的位置即可。其实就是在普通的计算过程中取模。
#include <cstdio> #include <cstdlib> #include <iostream> #include <cstring> using namespace std; typedef long long ll; inline char get(void) { static char buf[100000], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); if (p1 == p2) return EOF; } return *p1++; } template<typename T> inline int read(T &x) { static char c; x = 0; for (c = get(); c < '0' || c > '9'; c = get()); for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get()); return x; } template<typename T> inline T Min(T a, T b) { return a < b ? a : b; } const ll N = 100100, INF = 1e16; ll n, m, k, x, y, pos, Cnt, res; ll cnt[N << 2]; ll Calculate(ll x, ll y) { ll p = ((y - x) % n + n) % n; if (cnt[p] == -1) return INF; return m * cnt[p] + x; } int main(void) { read(m); read(n); read(k); n <<= 1; m <<= 1; memset(cnt, -1, sizeof cnt); while (1) { if (cnt[pos] != -1) break; cnt[pos] = Cnt++; pos = (pos + m) % n; } for (int i = 0; i < k; i++) { read(x); read(y); res = INF; res = Min(res, Calculate(x, y)); res = Min(res, Calculate(m - x, y)); res = Min(res, Calculate(x, n - y)); res = Min(res, Calculate(m - x, n - y)); if (res == INF) res = -1; printf("%I64d\n", res); } return 0; }