赞
踩
目录
其一:暴力枚举(太慢了)
其二:找规律,打表发现有循环,数一下是60,每60个里有8个满足题目的数
其一
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
-
- ll n = 202202011200;
- ll a, b, c;
- ll ans;
-
- int main() {
- a = 1, b = 1;
- for (int i = 3; i <= n; i++) {
- c = (a + b) % 10;
- if (c == 7) ans++;
- a = b, b = c;
- }
-
- cout << ans;
- return 0;
- }
其二
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
-
- ll n = 202202011200;
-
- ll ans;
-
- int main() {
- ans = n / 60 * 8;
-
- cout << ans;
- return 0;
- }
数据范围a小于1e6,打表发现最少31项就超过了1e6,也就是说只需要枚举前30项就行了,后面的必须改
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- const int N = 1e5 + 5;
- ll inf = 0x3f3f3f3f;
-
- int n;
- ll a[N];
- int res = inf;
- int ans;
-
- int main() {
- cin >> n;
- for (int i = 1; i <= n; i++) cin >> a[i];
- if (n > 30) {
- ans += n - 30;
- n = 30;
- }
-
- ll x, y, z;
- for (int i = 1; i <= 1e6; i++) {
- x = y = i;
- int cnt = 0;
- if (a[1] != x) cnt++;
- if (a[2] != y) cnt++;
- for (int j = 3; j <= n; j++) {
- z = x + y;
- if (a[j] != z) cnt++;
- x = y, y = z;
- }
- res = min(res, cnt);
- }
-
- cout << ans + res;
- return 0;
- }
如果一个区间内都是g的倍数,那么随便改一个为g,近似GCD为g
如果有一个不为g的倍数,那么把这个数改成g,近似GCD为g
把a中不是g的倍数标记为1,否则为0,则问题转化为求区间和小于等于1的区间个数
其一:枚举左右端点+前缀和优化 40分
其二:枚举左端点,二分找右端点100分(别忘了开long long)
其一
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 5;
-
- int n, g;
- int a[N], s[N];
- int ans;
-
- int main() {
- cin >> n >> g;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- if (a[i] % g == 0) a[i] = 0;
- else a[i] = 1;
- s[i] = s[i - 1] + a[i];
- }
-
- for (int l = 1; l <= n; l++) {
- for (int r = l + 1; r <= n; r++) {
- if (s[r] - s[l - 1] <= 1) ans++;
- }
- }
-
- cout << ans;
- return 0;
- }
其二
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- const int N = 1e5 + 5;
-
- int n, g;
- int a[N], s[N];
- ll ans;
-
- int find(int l, int r) {
- int x = l;
- while (l < r) {
- int mid = (l + r + 1) >> 1;
- if (s[mid] - s[x - 1] <= 1) l = mid;
- else r = mid - 1;
- }
- return l;
- }
-
- int main() {
- cin >> n >> g;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- if (a[i] % g == 0) a[i] = 0;
- else a[i] = 1;
- s[i] = s[i - 1] + a[i];
- }
-
- for (int l = 1; l <= n; l++) ans += find(l, n) - l;
-
- cout << ans;
- return 0;
- }
使用线段树维护26个标记,然后暴力下传即可
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define cl u << 1
- #define cr u << 1 | 1
- const int N = 1e5 + 5;
-
- string s;
- int n, m;
- int k = 26;
- struct node {
- int l, r;
- int v;
- int f[30];
- void init(int _l, int _r, char a) {
- l = _l, r = _r;
- v = a - 'a';
- for (int i = 0; i < k; i++) f[i] = i;
- }
- } t[N << 2];
-
- void build(int u, int l, int r) {
- if (l == r) t[u].init(l, r, s[l]);
- else {
- t[u].init(l, r, s[l]);
- int mid = (l + r) >> 1;
- build(cl, l, mid);
- build(cr, mid + 1, r);
- }
- }
-
- void pushdown(node &u, node &l, node &r) {
- for (int i = 0; i < k ; i++) l.f[i] = u.f[l.f[i]];
- for (int i = 0; i < k; i++) r.f[i] = u.f[r.f[i]];
- for (int i = 0; i < k; i++) u.f[i] = i;
- }
- void pushdown(int u) {
- pushdown(t[u], t[cl], t[cr]);
- }
-
- void modify(int u, int l, int r, int a, int b) {
- if (t[u].l >= l && t[u].r <= r) {
- for (int i = 0; i < k; i++) {
- if (t[u].f[i] == a) t[u].f[i] = b;
- }
- } else {
- pushdown(u);
- int mid = (t[u].l + t[u].r) >> 1;
- if (l <= mid) modify(cl, l, r, a, b);
- if (r > mid) modify(cr, l, r, a, b);
- }
- }
-
- void query(int u, int l, int r) {
- if (t[u].l == t[u].r) cout << char(t[u].f[t[u].v] + 'a');
- else {
- pushdown(u);
- int mid = (t[u].l + t[u].r) >> 1;
- if (l <= mid) query(cl, l, r);
- if (r > mid) query(cr, l, r);
- }
- }
-
- int main() {
- cin >> s >> m;
- n = s.size();
- s = ' ' + s;
- build(1, 1, n);
-
- int l, r;
- char a, b;
- while (m--) {
- cin >> l >> r >> a >> b;
- a -= 'a', b -= 'a';
- modify(1, l, r, a, b);
- }
-
- query(1, 1, n);
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。