#include<bits/stdc++.h>
#define LoveFurina ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e5 + 10;
int n, k;
int a[N];
set<int> mex;
int main() {
LoveFurina;
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
if (a[i]) {
mex.insert(0);
while (i < n && a[i + 1] >= a[i]) i++;
}
else {
mex.insert(1);
while (i < n && !a[i + 1]) i++;
for (int t = 1; t <= k && i <= n; ++t, ++i) {
mex.insert(a[i] + 1);
if (i >= n || a[i + 1] != a[i] + 1) break;
}
while (i < n && a[i + 1] >= a[i]) i++;
}
}
cout << mex.size();
return 0;
}P4861 按钮
BSGS,gcd,蓝
#include<bits/stdc++.h>
#define int long long
#define LoveFurina ios::sync_with_stdio(0), cin.tie(0), cin.tie(0)
using namespace std;
map<int, int> tag_y;
inline int qpow(int a, int b, int p) {
int ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % p;
a = (a * a) % p; b >>= 1;
}
return ans;
}
int BSGS(int a, int b, int p) {
if (a % p == b % p) return 1;
if (a % p == 0 && b) return -1;
int A = (int)ceil(sqrt(p)), tmp = qpow(a, A, p);
for(int i = 0, r_res = b; i <= A; ++i)
tag_y[r_res] = i, r_res = (r_res * a) % p;
for(int x = 1, l_res = 1; x <= A; ++x) {
l_res = (l_res * tmp) % p;
if(tag_y[l_res]) {
int res = x * A - tag_y[l_res];
if (!res) continue;
return res;
}
}
return -1;
}
signed main() {
LoveFurina;
int a, p;
cin >> p >> a;
if (__gcd(a, p) > 1) {
cout << "Let's go Blue Jays!";
return 0;
}
cout << BSGS(a, 1, p);
return 0;
}P4195 【模板】扩展 BSGS / exBSGS
exBSGS,紫
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int, int> tag_y;
inline void exgcd(int a, int &x, int b, int &y, int &gcd) {
if (b == 0) {
x = 1, y = 0, gcd = a;
return ;
}
int xx, yy;
exgcd(b, xx, a % b, yy, gcd);
x = yy;
y = xx - (a / b) * yy;
}
inline int inv(int a, int p) {
int x, y, gcd;
exgcd(a, x, p, y, gcd);
return (x % p + p) % p;
}
inline int qpow(int a, int b, int p) {
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % p;
a = (a * a) % p; b >>= 1;
}
return ans;
}
inline int BSGS(int a, int b, int p) {
tag_y.clear();
int A = (int)ceil(sqrt(p)), tmp = qpow(a, A, p);
for (int i = 0, r_res = b; i <= A; ++i)
tag_y[r_res] = i, r_res = (r_res * a) % p;
for (int x = 1, l_res = 1; x <= A; ++x) {
l_res = (l_res * tmp) % p;
if (tag_y.count(l_res)) return x * A - tag_y[l_res];
}
return -1;
}
inline int exBSGS(int a, int b, int p) {
a %= p, b %= p;
if (b == 1) return 0;
int x = 0, y = 0, gcd = 0;
exgcd(a, x, p, y, gcd);
int k = 0, m = 1;
while (gcd != 1) {
if (b % gcd) return -1;
++k;
b /= gcd, p /= gcd;
m = m * (a / gcd) % p;
if (m == b) return k;
exgcd(a, x, p, y, gcd);
}
int ans = BSGS(a, b * inv(m, p) % p, p);
return (ans == -1) ? -1 : ans + k;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, p;
while (cin >> a >> p >> b) {
if (a == 0 && p == 0 && b == 0) break;
int ans = exBSGS(a, b, p);
if (ans == -1) cout << "No Solution\n";
else cout << ans << '\n';
}
return 0;
}P3846 【模板】BSGS / [TJOI2007] 可爱的质数
BSGS,蓝
#include<bits/stdc++.h>
#define int long long
using namespace std;
map<int, int> tag_y;
inline int qpow(int a, int b, int p) {
int ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % p;
a = (a * a) % p; b >>= 1;
}
return ans;
}
int BSGS(int a, int b, int p) {
if (a % p == b % p) return 1;
if (a % p == 0 && b) return -1;
int A = (int)ceil(sqrt(p)), tmp = qpow(a, A, p);
for(int i = 0, r_res = b; i <= A; ++i)
tag_y[r_res] = i, r_res = (r_res * a) % p;
for(int x = 1, l_res = 1; x <= A; ++x) {
l_res = (l_res * tmp) % p;
if(tag_y[l_res]) return x * A - tag_y[l_res];
}
return -1;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, p;
cin >> p >> a >> b;
int ans = BSGS(a, b, p);
if (ans == -1) cout << "no solution";
else cout << ans;
return 0;
}
P2252 【模板】威佐夫博弈 / [SHOI2002] 取石子游戏
威佐夫博弈,紫
#include<bits/stdc++.h>
using namespace std;
int a, b;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> a >> b;
if (a > b) swap(a, b);
if (int((double)(b - a) * (1 + sqrt(5)) / 2) == a) cout << 0;
else cout << 1;
return 0;
}
P3868 [TJOI2009] 猜数字
数学,CRT,蓝
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int K = 20;
int k;
int a[K], b[K];
int lasta, lastb;
inline void exgcd(int a, int &x, int b, int &y, int &gcd) {
// ax + by = 1 * gcd
// b * x1 + (a % b) * y1 = 1 * gcd
if(b == 0) {
x = 1, y = 0, gcd = a;
return ;
}
int xx = 0, yy = 0;
exgcd(b, xx, a % b, yy, gcd);
x = yy;
y = xx - (a / b) * yy;
}
inline int excrt(int a1, int p1, int a2, int p2, int &lcm) {
// x == a1 (mod p1) == a2 (mod p2)
int s0 = 0, t0 = 0, gcd = 0;
exgcd(p1, s0, p2, t0, gcd);
lcm = p1 / gcd * p2;
// if((a1 - a2) % gcd) return -1;
int u = (a1 - a2) / gcd;
int t = t0 * u;
int x = a2 + t * p2;
return (x % lcm + lcm) % lcm;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> k;
for (int i = 1; i <= k; ++i) cin >> a[i];
for (int i = 1; i <= k; ++i) cin >> b[i];
lasta = a[1], lastb = b[1];
for (int i = 2; i <= k; ++i) {
int tmpa = lasta, tmpb = lastb;
lasta = excrt(tmpa, tmpb, a[i], b[i], lastb);
}
cout << lasta;
return 0;
}
已提交
P2284 [HNOI2003] 密室之门
exCRT,exGCD,紫
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int mod[N];
int rem[N];
inline void exgcd(int a, int &x, int b, int &y, int &gcd) {
if (b == 0) {
x = 1, y = 0, gcd = a;
return ;
}
exgcd(b, y, a % b, x, gcd);
y -= a / b * x;
}
inline bool check(int r1, int m1, int r2, int m2) {
int x, y, gcd;
exgcd(m1, x, m2, y, gcd);
return (r1 - r2) % gcd == 0;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while (n--) {
int a;
cin >> a;
if (a == 1) {
int b, p;
cin >> b >> p;
cout << "possible\n";
continue;
}
bool ok = 1;
for (int i = 1, b, p; i <= a; ++i) {
cin >> b >> p;
mod[i] = b;
rem[i] = (b - p) % b;
}
for (int i = 1; i <= a && ok; ++i)
for (int j = i + 1; j <= a && ok; ++j)
if (!check(rem[i], mod[i], rem[j], mod[j]))
ok = 0;
cout << (ok ? "possible\n" : "impossible\n");
}
return 0;
}
P2444 [POI 2000 R1] 病毒
AC自动机,紫
#include<bits/stdc++.h>
using namespace std;
const int N = 3e4 + 10;
int n;
int cnt = 0;
int tri[N][2];
int fail[N];
bool wrong[N];
int vis[N];
inline void insert(string s) {
int p = 0;
for (int i = 0; i < s.size(); ++i) {
int c = s[i] - '0';
if (!tri[p][c]) tri[p][c] = ++cnt;
p = tri[p][c];
}
wrong[p] = 1;
}
inline void build() {
queue<int> q;
for (int i = 0; i < 2; ++i)
if (tri[0][i])
q.push(tri[0][i]);
while (!q.empty()) {
int p = q.front(); q.pop();
for (int i = 0; i < 2; ++i) {
if (tri[p][i]) {
fail[tri[p][i]] = tri[fail[p]][i];
q.push(tri[p][i]);
if (wrong[tri[fail[p]][i]]) wrong[tri[p][i]] = 1;
}
else tri[p][i] = tri[fail[p]][i];
}
}
}
inline bool dfs(int p) {
if (vis[p] == 1) return 1;
else if (vis[p] == -1) return 0;
vis[p] = 1;
for (int i = 0; i < 2; ++i)
if (!wrong[tri[p][i]])
if (dfs(tri[p][i]))
return 1;
vis[p] = -1;
return 0;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
insert(s);
}
build();
if (dfs(0)) cout << "TAK";
else cout << "NIE";
return 0;
}
P3082 [USACO13MAR] Necklace G
AC自动机,蓝
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
const int M = 1e3 + 10;
const int INF = 0x3f3f3f3f;
string a, b;
int n, m;
int tri[M][30];
int kmp[M];
int dp[N][M];
int len = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> a >> b;
n = a.size(), m = b.size();
for (int i = 2, p = 0; i <= m; ++i) {
while (p && b[i - 1] != b[p]) p = kmp[p];
kmp[i] = (b[i - 1] == b[p]) ? ++p : 0;
}
for (int i = 0; i < m; ++i) {
for (int j = 1; j <= 26; ++j) {
int cur = i;
while (cur > 0 && b[cur] != (j + 'a' - 1)) cur = kmp[cur];
tri[i][j] = (b[cur] == (j + 'a' - 1)) ? cur + 1 : cur;
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
dp[i][j] = -INF;
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
if (i) {
for (int j = 0; j <= m; ++j)
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
}
for (int j = 0; j < m; ++j) {
if (dp[i][j] == -INF) continue;
int c = a[i] - 'a' + 1;
int p = tri[j][c];
if (p < m) dp[i + 1][p] = max(dp[i + 1][p], dp[i][j] + 1);
}
}
for (int i = 0; i < m; ++i)
len = max(len, dp[n][i]);
cout << n - len;
return 0 ;
}P3804 【模板】后缀自动机(SAM)
SAM自动机,紫
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
string s;
int pre;
long long ans;
vector<int> mp[N];
int cnt = 1;
int tri[N][26];
int lst = 1;
int con[N];
int len[N];
int siz[N];
inline void insert(int c) {
int last = lst;
int cur = ++cnt;
siz[cur] = 1;
len[cur] = len[last] + 1;
lst = cur;
int p = last;
for (; p && !tri[p][c]; p = con[p])
tri[p][c] = cur;
if (!p) {
con[cur] = 1;
return;
}
int q = tri[p][c];
if (len[p] + 1 == len[q]) {
con[cur] = q;
return;
}
int clone = ++cnt;
len[clone] = len[p] + 1;
memcpy(tri[clone], tri[q], sizeof(tri[q]));
con[clone] = con[q];
con[q] = con[cur] = clone;
for (; p && tri[p][c] == q; p = con[p])
tri[p][c] = clone;
}
void dfs(int u) {
for (int v : mp[u]) {
dfs(v);
siz[u] += siz[v];
}
if (siz[u] != 1)
ans = max(ans, 1LL * siz[u] * len[u]);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s;
for (pre = 0; pre < s.size(); ++pre)
insert(s[pre] - 'a');
for (int i = 2; i <= cnt; ++i)
mp[con[i]].push_back(i);
dfs(1);
cout << ans << '\n';
return 0;
}P6246 [IOI 2000] 邮局 加强版 加强版
wqs二分,黑
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 10;
const int INF = 5e11;
int n, m;
int a[N];
int sum[N];
struct Node {
int x, rpos;
} que[N];
int dp[N];
int cnt[N];
inline int cost(int l, int r) {
int pos = (l + r + 1) >> 1;
return (sum[r] - sum[pos]) - a[pos] * (r - pos) +
a[pos] * (pos - l) - (sum[pos] - sum[l]);
}
inline int find(int i, int j) {
int l = j, r = n + 1;
while (l < r - 1){
int mid = (l + r) >> 1;
if (dp[i] + cost(i, mid) < dp[j] + cost(j, mid)) l = mid;
else r = mid;
}
return r;
}
inline bool check(int mid) {
int h = 1, t = 0;
que[++t] = (Node){0, n + 1};
for (int i = 1; i <= n; ++i) {
while (h < t && que[h].rpos <= i) h++;
dp[i] = dp[que[h].x] + cost(que[h].x, i) + mid;
cnt[i] = cnt[que[h].x] + 1;
while (h < t && find(que[t].x, i) <= que[t - 1].rpos) t--;
que[t].rpos = find(que[t].x, i);
que[++t] = (Node){i, n + 1};
}
return cnt[n] >= m;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
int l = 0, r = INF;
while (l < r - 1) {
int mid = (l + r) >> 1;
if (check(mid)) l = mid;
else r = mid;
}
check(l);
cout << dp[n] - m * l;
return 0;
}
P1343 地震逃生
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 210;
const int INF = 1e18;
int n, m, s, t, x;
int dis[N];
int cur[N];
struct Node {
int to, val, rev;
Node(int t, int v, int r) :
to(t), val(v), rev(r) {}
};
vector<Node> adj[N];
inline void add(int u, int v, int w) {
adj[u].emplace_back(v, w, adj[v].size());
adj[v].emplace_back(u, 0, adj[u].size() - 1);
}
bool bfs() {
memset(dis, 0, sizeof(dis));
queue<int> q;
fill(cur, cur + N, 0);
q.push(s);
dis[s] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
for(auto e : adj[u]) {
int v = e.to, w = e.val;
if(w > 0 && !dis[v]) {
dis[v] = dis[u] + 1;
q.push(v);
if(v == t) return 1;
}
}
}
return 0;
}
inline int dinic(int u, int flow) {
if(u == t) return flow;
int res = flow;
for(int &i = cur[u]; i < adj[u].size(); ++i) {
Node &e = adj[u][i];
int v = e.to, w = e.val;
if(w > 0 && dis[u] + 1 == dis[v]) {
int get = dinic(v, min(res, w));
if(get == 0) continue;
e.val -= get;
adj[v][e.rev].val += get;
res -= get;
if(res == 0) break;
}
}
return flow - res;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> x;
s = 1, t = n;
for(int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
}
int ans = 0, flow = 0;
while(bfs()) {
while((flow = dinic(s, INF))) {
ans += flow;
}
}
if(!ans) cout << "Orz Ni Jinan Saint Cow!\n";
else cout << ans << " " << (x + ans - 1) / ans;
return 0;
}P2740 [USACO4.2] 草地排水 Drainage Ditches
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 210;
const int INF = 1e18;
int n, m, s, t;
int dis[N];
int cur[N];
struct Node {
int to, val, rev;
Node(int t, int v, int r) :
to(t), val(v), rev(r) {}
};
vector<Node> adj[N];
inline void add(int u, int v, int w) {
adj[u].emplace_back(v, w, adj[v].size());
adj[v].emplace_back(u, 0, adj[u].size() - 1);
}
bool bfs() {
memset(dis, 0, sizeof(dis));
queue<int> q;
fill(cur, cur + N, 0);
q.push(s);
dis[s] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
for(auto e : adj[u]) {
int v = e.to, w = e.val;
if(w > 0 && !dis[v]) {
dis[v] = dis[u] + 1;
q.push(v);
if(v == t) return 1;
}
}
}
return 0;
}
inline int dinic(int u, int flow) {
if(u == t) return flow;
int res = flow;
for(int i = cur[u]; i < adj[u].size(); ++i) {
Node &e = adj[u][i];
int v = e.to, w = e.val;
if(w > 0 && dis[u] + 1 == dis[v]) {
int get = dinic(v, min(res, w));
if(get == 0) continue;
e.val -= get;
adj[v][e.rev].val += get;
res -= get;
if(res == 0) break;
}
}
return flow - res;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
s = 1, t = m;
for(int i = 1, u, v, w; i <= n; ++i) {
cin >> u >> v >> w;
add(u, v, w);
}
int ans = 0, flow = 0;
while(bfs()) {
while((flow = dinic(s, INF))) {
ans += flow;
}
}
cout << ans;
return 0;
}P1896 [SCOI2005] 互不侵犯
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 10;
int n, k;
vector<int> sta, cnt;
int dp[N][1 << N][N * N];
inline bool ok(int s) {
return !(s & (s << 1));
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int s = 0; s < (1 << n); s++) {
if (ok(s)) {
sta.push_back(s);
cnt.push_back(__builtin_popcount(s));
}
}
int m = sta.size();
for (int i = 0; i < m; i++) {
int s = sta[i], c = cnt[i];
if (c <= k) dp[1][s][c] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j < m; j++) {
int s1 = sta[j], c1 = cnt[j];
for (int l = 0; l < m; l++) {
int s2 = sta[l];
if ((s1 & s2) != 0 || ((s1 << 1) & s2) || ((s1 >> 1) & s2)) continue;
for (int ci = 0; ci + c1 <= k; ci++)
dp[i][s1][ci + c1] += dp[i-1][s2][ci];
}
}
}
int ans = 0;
for (int i = 0; i < m; i++) {
int s = sta[i];
ans += dp[n][s][k];
}
cout << ans << endl;
return 0;
}