A Sanitize Hands
问题:
思路:前缀和,暴力,你想咋做就咋做
代码:
#include <iostream>using namespace std;const int N = 2e5 + 10;int n, m;
int a[N];int main() {cin >> n >> m;for(int i = 1; i <= n; i ++ ) {cin >> a[i];}int ans = 0;for(int i = 1; i <= n; i ++ ) {m -= a[i];ans = i;if(m <= 0) break;}if(m < 0) cout << ans - 1;else cout << ans;return 0;
}
B Uppercase and Lowercase
问题:
思路:大小写转换,这里有个问题,为什么我的转换最后都变成数字了,先留个疑问
代码:
#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int N = 2e5 + 10;string str;int main() {cin >> str;int cnt1 = 0, cnt2 = 0;for(auto t: str) {if(t >= 'a' && t <= 'z') cnt1 ++;else cnt2 ++;}if(cnt1 >= cnt2)transform(str.begin(),str.end(),str.begin(),::tolower);else transform(str.begin(),str.end(),str.begin(),::toupper);cout<<str<<endl;return 0;
}
C Sierpinski carpet
问题:
思路:阴间题,第一眼递归,但是不想求太多坐标,于是想到把图全变成‘#’最后填充'.'
代码:
#include <iostream>
#include <cmath>
#include <vector>using namespace std;const int N = pow(3, 6) + 10;char g[N][N];
int n;int main() {cin >> n;int len = pow(3, n);for(int i = 1; i <= len; i ++ ) {for(int j = 1; j <= len; j ++ ) {g[i][j] = '#';}}for(int level = 1; level <= n; level ++ ) {for(int i = 1 + pow(3, level - 1); i <= len; i += pow(3, level)) {for(int j = 1 + pow(3, level - 1); j <= len; j += pow(3, level)) {for(int k = i; k <= i + pow(3, level - 1) - 1; k ++ ) {for(int u = j; u <= j + pow(3, level - 1) - 1; u ++ ) {g[k][u] = '.';}}}}}for(int i = 1; i <= len; i ++ ) {for(int j = 1; j <= len; j ++ ) {cout << g[i][j];}cout << endl;}return 0;
}
D 88888888
问题:
思路:逆元,快速幂,对原式子变形后发现最后的结果实际上就是x 乘上一个等比数列,这是碰见的第一道逆元的题目,也明确了我对逆元的认识,由于 a / b % mod != (a % mod/ b % mod) % mod,而直接除的话会造成精度丢失,因此我们可以把除法变成乘法,根据费马小定理如果b和p互质,那么b的逆元就等于b ^ p - 2 因此可以快速幂求逆元
代码:
#include <iostream>using namespace std;const int mod = 998244353;long long x;int get(long long a) {int cnt = 0;while(a) {a /= 10;cnt ++;}return cnt;
}long long qmi(long long a, long long b) {long long res = 1;while(b) {if(b & 1) res = ((res % mod) * (a % mod)) % mod;b >>= 1;a = (a % mod * a % mod) % mod;}return res;
}int main() {cin >> x;int len = get(x);long long part1 = x % mod;long long a = qmi(10, (long long)len);long long b = qmi(a, x);b --;long long c = qmi(a - 1, 998244353 - 2);long long part2 = (b % mod * c % mod) % mod;cout << (part1 * part2) % mod;return 0;
}
E Reachability in Functional Graph
问题:
思路:考虑如果题目是一颗树的话那么直接一个记忆化即可,但是该题会出现环,因此考虑缩点,记得开long long
据说这是基环树板子,回头学一下基环树
代码:
#include <iostream>
#include <cstring>
#include <stack>
#include <map>using namespace std;const int N = (2e5 + 10) * 2;stack<int> stk;
int n;
int val[N], ne[N], h[N], idx;
int dfn[N], low[N], id[N], _size[N], scc_cnt, ts;
int cnt[N];
bool ins[N], st[N];
long long ans = 0;void add(int a, int b) {val[idx] = b;ne[idx] = h[a];h[a] = idx ++;
}void tarjan(int u) {dfn[u] = low[u] = ++ ts;stk.push(u);ins[u] = true;for(int i = h[u]; i != -1; i = ne[i]) {int j = val[i];if(!dfn[j]) {tarjan(j);low[u] = min(low[u], low[j]);} else if(ins[j]) low[u] = min(low[u], dfn[j]);}if(dfn[u] == low[u]) {++ scc_cnt;int y;do {y = stk.top();stk.pop();ins[y] = false;id[y] = scc_cnt;_size[scc_cnt] ++;} while (y != u);}
}void dfs(int u) {for(int i = h[u]; i != -1; i = ne[i]) {int j = val[i];if(!st[j]) {dfs(j);st[j] = true;}cnt[u] += cnt[j];ans += _size[u] * cnt[j];}
}int main() {memset(h, -1, sizeof h);cin >> n;scc_cnt = n;for(int i = 1; i <= n; i ++ ) {int x;cin >> x;add(i, x);}for(int i = 1; i <= n; i ++ ) if(!dfn[i]) tarjan(i);for(int i = 1; i <= n; i ++ ) cnt[id[i]] = _size[id[i]];map<pair<int, int>, int> ma;for(int i = 1; i <= n; i ++ ) {for(int j = h[i]; j != -1; j = ne[j]) {int k = val[j];if(id[i] != id[k] && !ma[{i, k}]) {add(id[i], id[k]);ma[{i, k}] ++;}}}memset(st, 0, sizeof st);for(int i = scc_cnt; i > n; i -- ) {if(!st[i]) {st[i] = true;dfs(i);}}for(int i = scc_cnt; i > n; i -- ) ans += (long long)_size[i] * (_size[i] - 1);cout << ans + n;return 0;
}
F two sequence queries
题目:
思路:对sigema a*b做一点变形 设a加上了x,b加上了y
原式 = sigema (a + x) (b + y) = sigema a * b + y * a + x * b + x * y
于是题目变成了区间修改区间查询,显然线段树lazytag板子
代码:这里代码只a了21个数据,应该是哪里没有mod到位或者什么细节没有注意到,短时间内不改了,到期末了
#include <iostream>using namespace std;const int N = 2e5 + 10;
const int mod = 998244353;int n, m;
struct node{unsigned long long l, r;unsigned long long suma, sumb, sumab;unsigned long long taga, tagb;
}tr[4 * N];void pushup(int u) {tr[u].suma = (tr[u << 1].suma + tr[u << 1 | 1].suma) % mod;tr[u].sumb = (tr[u << 1].sumb + tr[u << 1 | 1].sumb) % mod;tr[u].sumab = (tr[u << 1].sumab + tr[u << 1 | 1].sumab) % mod;
}void pushdown(int u) {tr[u << 1].sumab = (tr[u << 1].sumab + tr[u].taga * tr[u << 1].sumb + tr[u].tagb * tr[u << 1].suma + tr[u].taga * tr[u].tagb * (tr[u << 1].r - tr[u << 1].l + 1)) % mod;tr[u << 1 | 1].sumab = (tr[u << 1 | 1].sumab + tr[u].taga * tr[u << 1 | 1].sumb + tr[u].tagb * tr[u << 1 | 1].suma + tr[u].taga * tr[u].tagb * (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1)) % mod;tr[u << 1].suma = (tr[u << 1].suma + (tr[u << 1].r - tr[u << 1].l + 1) * tr[u].taga) % mod;tr[u << 1 | 1].suma = (tr[u << 1 | 1].suma + (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) * tr[u].taga) % mod;tr[u << 1].taga = (tr[u << 1].taga + tr[u].taga) % mod;tr[u << 1 | 1].taga = (tr[u << 1 | 1].taga + tr[u].taga) % mod;tr[u].taga = 0;tr[u << 1].sumb = (tr[u << 1].sumb + (tr[u << 1].r - tr[u << 1].l + 1) * tr[u].tagb) % mod;tr[u << 1 | 1].sumb = (tr[u << 1 | 1].sumb + (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) * tr[u].tagb) % mod;tr[u << 1].tagb = (tr[u << 1].tagb + tr[u].tagb) % mod;tr[u << 1 | 1].tagb = (tr[u << 1 | 1].tagb + tr[u].tagb) % mod;tr[u].tagb = 0;
}void build(int u, int l, int r) {tr[u].l = l, tr[u].r = r;if(l == r) return;int mid = l + r >> 1;build(u << 1, l, mid);build(u << 1 | 1, mid + 1, r);
}void add(int u, int p, int x, int type) {if(tr[u].l == tr[u].r) {if(type == 1) tr[u].suma = x;else if(type == 2) tr[u].sumb = x;tr[u].sumab = (tr[u].suma * tr[u].sumb) % mod;} else {int mid = tr[u].l + tr[u].r >> 1;if(p <= mid) add(u << 1, p, x, type);else add(u << 1 | 1, p, x, type);pushup(u);}
}void modify(int u, int l, int r, unsigned long long d, int type) {if(tr[u].l >= l && tr[u].r <= r) {if(type == 1) {tr[u].suma = (tr[u].suma + d * (tr[u].r - tr[u].l + 1)) % mod;tr[u].taga = (tr[u].taga + d) % mod;tr[u].sumab = (tr[u].sumab + d * tr[u].sumb) % mod;} else if(type == 2) {tr[u].sumb = (tr[u].sumb + d * (tr[u].r - tr[u].l + 1)) % mod;tr[u].tagb = (tr[u].tagb + d) % mod;tr[u].sumab = (tr[u].sumab + d * tr[u].suma) % mod;}} else {pushdown(u);int mid = tr[u].l + tr[u].r >> 1;if(mid >= l) modify(u << 1, l, r, d, type);if(mid < r) modify(u << 1 | 1, l, r, d, type);pushup(u);}
}long long query(int u, int l, int r) {if(tr[u].l >= l && tr[u].r <= r) {return tr[u].sumab;} else {pushdown(u);int mid = tr[u].l + tr[u].r >> 1;long long res = 0;if(mid >= l) res = query(u << 1, l, r);if(mid < r) res = (res + query(u << 1 | 1, l, r)) % mod;return res;}
}int main() {cin >> n >> m;build(1, 1, n);for(int i = 1; i <= n; i ++ ) {int x;cin >> x;add(1, i, x % mod, 1);} for(int i = 1; i <= n; i ++ ) {int x;cin >> x;add(1, i, x % mod, 2);}while(m -- ) {int op;cin >> op;if(op == 1) {int l, r;unsigned long long d;cin >> l >> r >> d;modify(1, l, r, d % mod, 1);} else if(op == 2) {int l, r, d;cin >> l >> r >> d;modify(1, l, r, d % mod, 2);} else {int l, r;cin >> l >> r;cout << query(1, l, r) << endl;}}return 0;
}