解题思路:
变化的规律为红 -> 绿 -> 蓝 -> 红
将红、绿、蓝分别表示0、1、2
原问题就转化为求 n 的三进制表示的最低的 m 位,即求n mod 3^m的三进制表示
复杂度 O(m)
#include <iostream> #include <cstdio> using namespace std; typedef long long LL; const int maxn = 30 + 5; char s[maxn]; int m; LL n; int main() { // freopen("in.txt", "r", stdin); int T; while (scanf("%d", &T) != EOF) { while (T--) { cin>>m>>n; for (int i=0; i<m; ++i) { s[i] = 'R'; } LL x = n%3; LL y = n/3; for (int i=m-1; i>=0; --i) { if (x == 1) { s[i] = 'G'; } else if (x == 2) { s[i] = 'B'; } if (y == 0) { break; } x = y%3; y /= 3; } for (int i=0; i<m; ++i) { cout<<s[i]; } cout<<endl; } } return 0; }
解题思路:
暴力肯定是不可行的
可以用两个数组记录行和列交换的操作
再用两个数组记录行和列加一个数的操作
复杂度 O(mn + q)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1000 + 10; int Matrix[maxn][maxn]; int row[maxn], row_add[maxn]; //行 int column[maxn], column_add[maxn]; //列 int main() { // freopen("in.txt", "r", stdin); int T; while (scanf("%d", &T) != EOF) { while (T--) { int n, m, q; scanf("%d%d%d", &n, &m, &q); for (int i=1; i<=n; ++i) { for (int j=1; j<=m; ++j) { scanf("%d", &Matrix[i][j]); } } int a, x, y; memset(row_add, 0, sizeof(row_add)); memset(column_add, 0, sizeof(column_add)); for (int i=1; i<=n; ++i) { row[i] = i; } for (int i=1; i<=m; ++i) { column[i] = i; } for (int i=0; i<q; ++i) { scanf("%d%d%d", &a, &x, &y); if (a == 1) { int t = row[x]; row[x] = row[y]; row[y] = t; } else if (a == 2) { int t = column[x]; column[x] = column[y]; column[y] = t; } else if (a == 3) { row_add[row[x]] += y; } else { column_add[column[x]] += y; } } int t; for (int i=1; i<=n; ++i) { for (int j=1; j<m; ++j) { t = Matrix[row[i]][column[j]] + row_add[row[i]] + column_add[column[j]]; printf("%d ", t); } t = Matrix[row[i]][column[m]] + row_add[row[i]] + column_add[column[m]]; printf("%d\n", t); } } } return 0; }
题意:
是否存在正整数 x 和 y,满足 n = x^2 - y^2 (n 为正整数)
解题思路:
据说这次 BC 出现很多问题(导致unrated)所以肯定不知道哪里有毒
一:
n = x^2 - y^2 = (x+y) * (x-y)
令 a = x+y; b = x-y;
那么 n = a*b; x = (a+b) / 2; y = (a-b) / 2;
所以 (a-b) != 0 && (a-b) %2 == 0 时有解
复杂度O(sqrt(n))
以为会TLE,没想到358MS过了
二:
假设存在解时 y = x+i; n = (x+i)^2 - x^2;
那么 n = 2xi + i^2;
i = 1时,n = 2x + 1;
i = 2时,n = 4x + 4;
容易得出结论,当 n 不等于 1 && n 不等于 4 && n 为奇数或者 4 的倍数时,方程一定有正整数解
#include <iostream> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; int main() { // freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while (T--) { LL n; scanf("%I64d", &n); bool flag = false; int t = (int)sqrt(n); for (int i=1; i<=t; ++i) { if (n%i == 0) { LL a = i; LL b = n / i; if ((a-b) != 0 && (a-b)%2 == 0) { // cout<<a<<" "<<b<<endl; flag = true; break; } } } if (flag) { printf("True\n"); } else { printf("False\n"); } } return 0; }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; typedef long long LL; int main() { // freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while (T--) { LL n; scanf("%I64d", &n); if (n != 1 && n != 4 && (n%2 != 0 || n%4 == 0)) { printf("True\n"); } else { printf("False\n"); } } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> using namespace std; typedef long long LL; const int maxn = 100; int t[maxn]; int main() { #ifdef __AiR_H freopen("in.txt", "r", stdin); #endif // __AiR_H int n; scanf("%d", &n); t[0] = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &t[i]); } ++n; t[n] = 90; bool flag = false; for (int i = 1; i <= n; ++i) { if (t[i] - t[i-1] > 15) { flag = true; printf("%d\n", t[i-1] + 15); break; } } if (!flag) { printf("90\n"); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <vector> using namespace std; typedef long long LL; const int maxn = 1e5 + 10; vector<int> Div1, Div2; int main() { int n, m; scanf("%d%d", &n, &m); int u, v; for (int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); if (u < v) { swap(u, v); } Div1.push_back(u); Div2.push_back(v); } if (m == 0) { printf("%d\n", n-1); } else { int a = *max_element(Div2.begin(), Div2.end()); int b = *min_element(Div1.begin(), Div1.end()); printf("%d\n", max(0, b-a)); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <vector> using namespace std; typedef long long LL; const int maxn = 1e5 + 10; int main() { #ifdef __AiR_H freopen("in.txt", "r", stdin); #endif // __AiR_H int n, m; scanf("%d%d", &n, &m); int u, v; int div2_Max = 1, div1_Min = n; for (int i = 0; i < m; ++i) { scanf("%d%d", &u, &v); if (u < v) { swap(u, v); } div1_Min = min(div1_Min, u); div2_Max = max(div2_Max, v); } if (m == 0) { printf("%d\n", n-1); } else { printf("%d\n", max(0, div1_Min - div2_Max)); } return 0; }