目录
1、智乃酱的区间乘积
tag:线段树 逆元 前缀和
https://ac.nowcoder.com/acm/problem/225632
//a ber
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
int n, m, a[N], s[N];
int fp(int a, int b, int mod) {
int ok = 1;
while(b) {
if(b & 1) ok = (ok * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ok;
}
int getinv(int x) {
return fp(x, mod - 2, mod);
}
signed main() {
scanf("%lld %lld", &n, &m);
s[0] = 1;
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for(int i = 1; i <= n; i++) s[i] = (s[i - 1] * a[i]) % mod;
for(int l, r, i = 0; i < m; i++) {
scanf("%lld%lld", &l, &r);
printf("%lld\n", s[r]*getinv(s[l - 1]) % mod);
}
return 0;
}
2、牛牛的猜球游戏
tag:前缀和
https://ac.nowcoder.com/acm/problem/212925
//a ber
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, f[N][10], t[10];
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = 0; i < 10; i++) f[0][i] = i;
for(int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
for(int j = 0; j < 10; j++) f[i][j] = f[i - 1][j];
swap(f[i][x], f[i][y]);
}
for(int l, r, _ = 0; _ < m; _++) {
cin >> l >> r;
for(int i = 0; i < 10; i++) t[f[l - 1][i]] = i;
for(int i = 0; i < 10; i++) printf("%d ", t[f[r][i]]);
puts("");
}
return 0;
}
3、智乃酱的双塔问题
https://ac.nowcoder.com/acm/problem/223137
智乃的源码,自己写了一遍,还是有很多地方能理解,但是推不出来
//a ber
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int max_mat = 2;
//矩阵
struct Mat {
int a[2][2];
//单位矩阵
Mat() {
for(int i = 0; i < max_mat; i++) {
for(int j = 0; j < max_mat; j++) {
a[i][j] = 0;
}
}
for(int i = 0; i < max_mat; i++) a[i][i] = 1;
}
Mat(int a1, int a2, int a3, int a4) {
a[0][0] = a1;
a[0][1] = a2;
a[1][0] = a3;
a[1][1] = a4;
}
};
int fp(int a, int b, int mod) {
int ok = 1;
while(b) {
if(b & 1) ok = (ok * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ok;
}
int get_inv(int x) {
return fp(x, mod - 2, mod);
}
int A[max_mat][max_mat << 1];
void row_minus(int a, int b, int k) {
for(int i = 0; i < 2 * max_mat; i++) {
A[a][i] = (A[a][i] - A[b][i] * k % mod) % mod;
if(A[a][i] < 0) A[a][i] += mod;
}
return;
}
void row_multiplies(int a, int k) {
for(int i = 0; i < 2 * max_mat; i++) {
A[a][i] = (A[a][i] * k) % mod;
}
return;
}
void row_swap(int a, int b) {
for(int i = 0; i < 2 * max_mat; i++) {
swap(A[a][i], A[b][i]);
}
}
//求矩阵的逆
Mat getinv(Mat x) {
memset(A, 0, sizeof A);
for(int i = 0; i < max_mat; i++) {
for(int j = 0; j < max_mat; j++) {
A[i][j] = x.a[i][j];
A[i][max_mat + j] = i == j;
}
}
for(int i = 0; i < max_mat; i++) {
if(!A[i][i]) {
for(int j = i + 1; j < max_mat; j++) {
if(A[i][j]) {
row_swap(i, j);
break;
}
}
}
row_multiplies(i, get_inv(A[i][i]));
for(int j = i + 1; j < max_mat; j++) {
row_minus(j, i, A[j][i]);
}
}
for (int i = max_mat - 1; i >= 0; --i) {
for (int j = i - 1; j >= 0; --j) {
row_minus(j, i, A[j][i]);
}
}
Mat ret;
for (int i = 0; i < max_mat; ++i) {
for (int j = 0; j < max_mat; ++j) {
ret.a[i][j] = A[i][max_mat + j];
}
}
return ret;
}
const int N = 1e5 + 5;
const Mat tA(1, 1, 0, 1);
const Mat tB(1, 0, 1, 1);
Mat operator * (Mat x, Mat y) {
Mat c;
for(int i = 0; i < max_mat; i++) {
for(int j = 0; j < max_mat; j++) {
c.a[i][j] = 0;
}
}
for (int i = 0; i < max_mat; ++i) {
for (int j = 0; j < max_mat; ++j) {
for (int k = 0; k < max_mat; ++k) {
c.a[i][j] = (c.a[i][j] + x.a[i][k] * y.a[k][j] % mod) % mod;
}
}
}
return c;
}
Mat p[N];
char s[N];
int n, m, hs, ht, ps, pt;
signed main() {
scanf("%lld%lld", &n, &m);
scanf("%s", s + 1);
p[0] = Mat(1, 0, 0, 1);
for(int i = 1; i < n; i++) {
s[i] == '/' ? p[i] = p[i - 1] * tA : p[i] = p[i - 1] * tB;
}
for(int i = 0; i < m; i++) {
scanf("%lld%lld%lld%lld", &hs, &ht, &ps, &pt);
Mat ans = getinv(p[hs - 1]) * p[ht - 1];
printf("%lld\n", ans.a[ps][pt]);
}
return 0;
}
4、NOIP积木大赛 道路铺设
https://ac.nowcoder.com/acm/problem/16534
https://ac.nowcoder.com/acm/problem/21466
tag:差分,每次铺设都是进行差分,
差分数组有一个性质:差分数组中正数的和等于负数和,差分实际上是前缀和的逆
//a ber
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 7;
int n, a[N], d[N];
signed main() {
scanf("%lld", &n);
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
d[1] = a[1];
for(int i = 2; i <= n; i++) {
d[i] = a[i] - a[i - 1];
}
int ok = d[1];
for(int i = 2; i <= n; i++) if(d[i] > 0) ok += d[i];
printf("%lld", ok);
return 0;
}
5、Dragon of Loowater
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2267
tag: 双指针 贪心
蓝书每日一水,慢慢看
//a ber
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 7;
int n, m, a[N], b[N];
signed main() {
while(scanf("%d%d", &n, &m)) {
if(n == 0 && m == 0) break;
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 1; i <= m; i++) scanf("%d", &b[i]);
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + m);
int ok = 0, i, j;
for(i = 1, j = 1; i <= n && j <= m;) {
if(b[j] >= a[i]) {
ok += b[j];
j++;
i++;
} else {
j++;
}
}
i == n + 1 ? printf("%d\n", ok) : puts("Loowater is doomed!");
}
return 0;
}
6、Commando War
https://vjudge.net/problem/UVA-11729
tag:贪心
水水水
//a ber
#include <bits/stdc++.h>
using namespace std;
struct node {
int b, c;
bool operator<(const node& t)const {
return c > t.c;
}
};
int n, f;
vector<node> a;
signed main() {
while(scanf("%d", &n)) {
if(n == 0) break;
for(int x, y, i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
a.push_back(node{x, y});
}
sort(a.begin(), a.end());
int ok = 0, s = 0;
for(int i = 0; i < n; i++) {
s += a[i].b;
ok = max(ok, s + a[i].c);
}
printf("Case %d: %d\n", ++f, ok);
a.clear();
}
return 0;
}
7、牛牛的Link Power I
https://ac.nowcoder.com/acm/contest/19483/G
tag:前缀和
两次前缀和算贡献度
//a ber
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 9;
int n, p[N];
char s[N];
signed main() {
scanf("%lld", &n);
scanf("%s", s);
for(int i = 0; i < n; i++) {
if(s[i] == '1') p[i + 1]++;
}
for(int i = 1; i < n; i++) {
p[i] = (p[i] + p[i - 1]) % mod;
}
for(int i = 1; i < n; i++) {
p[i] = (p[i] + p[i - 1]) % mod;
}
int ok = 0;
for(int i = 1; i < n; i++) {
if(s[i] == '1') {
ok = (ok + p[i]) % mod;
}
}
printf("%lld",ok);
return 0;
}
8、小w的糖果
https://ac.nowcoder.com/acm/contest/19483/H
tag:差分
//a ber
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
int n, m, id, pos, d1[N], d2[N], d3[N];
void pre(int a[]) {
for(int i = 1; i <= n; ++i) {
a[i] = (a[i] + a[i - 1]) % mod;
}
return;
}
signed main() {
int t;
scanf("%lld", &t);
while(t-- && scanf("%lld%lld", &n, &m)) {
memset(d1, 0, sizeof d1);
memset(d2, 0, sizeof d2);
memset(d3, 0, sizeof d3);
for(int i = 0; i < m; i++) {
scanf("%lld%lld", &id, &pos);
if(id == 1) {
d1[pos]++;
}
if(id == 2) {
d2[pos]++;
}
if(id == 3) {
d3[pos]++;
d3[pos + 1]++;
}
}
pre(d1);
pre(d2);
pre(d2);
pre(d3);
pre(d3);
pre(d3);
for(int i = 1; i <= n; i++) {
int ok = (d1[i] + d2[i] + d3[i]) % mod;
i == n ? printf("%lld\n", ok) : printf("%lld ", ok);
}
}
return 0;
}