Skip to content

Commit 5fea51b

Browse files
committed
v1.1.1:增加了部分数论算法和修改了若干错误
1 parent 0e7164b commit 5fea51b

12 files changed

Lines changed: 1426 additions & 117 deletions

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**当前最新普通版发布版本为 `v1.1.0`****最新打印版发布版本为 `v1.1.0` (总词数约9.0w(含代码))**
1+
**当前最新普通版发布版本为 `v1.1.1`****最新打印版发布版本为 `v1.1.0` (总词数约9.0w(含代码))**
22

33
成品为 `template.pdf` (移步 [releases](https://github.com/lr580/algorithm_template/releases) 查看/下载)
44

@@ -46,6 +46,23 @@
4646

4747
## 更新日志
4848

49+
- 22/10/24 - 22/11/11 (`v1.1.1`)
50+
51+
- 添加了树上随机游走
52+
- 添加了带权并查集例题
53+
- 添加了负环输出方案模板
54+
- 添加了最短路少量内容
55+
- 修改了 STL priority\_queue 的错误描述
56+
- 微调了 tarjan 模板代码
57+
- 添加了高阶前缀和公式
58+
- 添加了错位排列数少量内容
59+
- 添加了 BSGS, exBSGS 和 exGCD 的另一种实现模板
60+
- 添加了中国剩余定理少量内容
61+
- 添加了矩阵快速幂常见建模例子
62+
- 添加了 exLucas 模板
63+
- 修改了同余一条性质的错误表述
64+
- 添加了 STL multiset 部分内容
65+
4966
- 22/10/19 - 22/10/20 (`v1.1.0`)
5067

5168
- 添加了哈密顿图结论

code/p2294_diff_constraints.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
const ll mn = 1e2 + 10, inf = 1e9;
5+
ll n, d[mn], m, vis[mn], cnt[mn];
6+
vector<pair<ll, ll>> e[mn];
7+
void solve()
8+
{
9+
cin >> n >> m;
10+
for (ll i = 0; i <= n; ++i)
11+
{
12+
e[i].clear();
13+
}
14+
for (ll i = 1, a, b, c; i <= m; ++i)
15+
{
16+
cin >> a >> b >> c;
17+
e[b].push_back({a - 1, -c});
18+
e[a - 1].push_back({b, c});
19+
}
20+
fill_n(d, n + 3, inf), fill_n(vis, n + 3, 0), fill_n(cnt, n + 3, 0);
21+
auto spfa = [&](ll s)
22+
{
23+
d[s] = 0, vis[s] = true;
24+
queue<ll> q;
25+
q.push(s);
26+
while (!q.empty())
27+
{
28+
ll u = q.front();
29+
vis[u] = false;
30+
q.pop();
31+
for (auto pr : e[u])
32+
{
33+
ll v = pr.first, w = pr.second;
34+
if (d[v] > d[u] + w)
35+
{
36+
d[v] = d[u] + w;
37+
if (!vis[v])
38+
{
39+
if (++cnt[v] >= n + 1)
40+
{
41+
return false;
42+
}
43+
vis[v] = true;
44+
q.push(v);
45+
}
46+
}
47+
}
48+
}
49+
return true;
50+
};
51+
bool fail = false;
52+
for (ll i = 0; i <= n; ++i)
53+
{
54+
if (!cnt[i])
55+
{
56+
fail |= spfa(i) == false;
57+
}
58+
}
59+
cout << (fail ? "false\n" : "true\n");
60+
}
61+
signed main()
62+
{
63+
cin.tie(0)->ios::sync_with_stdio(false);
64+
ll t;
65+
cin >> t;
66+
while (t--)
67+
{
68+
solve();
69+
}
70+
return 0;
71+
}

code/p2294_disjoint_set.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<bits/stdc++.h>
2+
int fa[105],cha[105];
3+
int find(int x)
4+
{
5+
if(x!=fa[x])
6+
{
7+
int t=find(fa[x]);
8+
cha[x]+=cha[fa[x]];
9+
fa[x]=t;
10+
}
11+
return fa[x];
12+
}
13+
int main()
14+
{
15+
int T,n,m,i,x,y,z,flag;
16+
scanf("%d",&T);
17+
while (T--)
18+
{
19+
flag=0;
20+
scanf("%d%d",&n,&m);
21+
for(i=0;i<=n;i++)
22+
{
23+
fa[i]=i;
24+
cha[i]=0;
25+
}
26+
for(i=1;i<=m;i++)
27+
{
28+
scanf("%d%d%d",&x,&y,&z);
29+
x--;
30+
if(find(x)!=find(y))
31+
{
32+
cha[fa[y]]=cha[x]-cha[y]-z;
33+
fa[fa[y]]=fa[x];
34+
}
35+
else
36+
if(cha[x]-cha[y]!=z) flag=1;
37+
}
38+
if(flag==0) printf("true\n"); else printf("false\n");
39+
}
40+
return 0;
41+
}

code/p2294_greedy.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<queue>
2+
#include<cmath>
3+
#include<cstdio>
4+
#include<cstring>
5+
#include<iostream>
6+
#include<algorithm>
7+
#define N 1100
8+
using namespace std;
9+
inline void read(int &x)
10+
{
11+
x=0;
12+
int p=1;
13+
char c=getchar();
14+
while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
15+
while(isdigit(c)) {x=(x<<1)+(x<<3)+(c^'0');c=getchar();}
16+
x*=p;
17+
18+
}//快速读入
19+
20+
int n,m;
21+
struct node
22+
{
23+
int l,r,s;
24+
bool operator < (const node &h)const
25+
{
26+
if(l!=h.l)return l>h.l;
27+
return r>h.r;
28+
29+
}//重载运算符,确定优先队列的优先级
30+
31+
}tmp;
32+
priority_queue<node>q;
33+
int main()
34+
{
35+
int t;
36+
read(t);
37+
while(t--)
38+
{
39+
while (!q.empty()) q.pop();//多组数据清空
40+
read(n);read(m);
41+
if(m==1){printf("true\n");continue;}//其实没必要特判,只是优化一点点
42+
for(int i=1;i<=m;i++)
43+
{
44+
int l,r,s;
45+
read(tmp.l);read(tmp.r);read(tmp.s);
46+
q.push(tmp);
47+
}
48+
tmp=q.top();//取出第一个
49+
q.pop();
50+
while(!q.empty())
51+
{
52+
node tmp1;
53+
tmp1=q.top();
54+
q.pop();
55+
if(tmp.l==tmp1.l)
56+
{
57+
if(tmp.r==tmp1.r)
58+
{
59+
if(tmp.s!=tmp1.s)
60+
{printf("false\n");goto end;}//退出多重循环的小操作
61+
}
62+
else
63+
if(tmp.r<tmp1.r)
64+
q.push((node) {tmp.r+1, tmp1.r, tmp1.s - tmp.s});//将抵消后的部分放入队列
65+
}
66+
tmp = tmp1;//继续比
67+
}
68+
printf("true\n");
69+
end:;
70+
}
71+
return 0;
72+
}

code/p3846_bsgs2.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
ll bsgs(ll a, ll b, ll p)
5+
{ // min x of a^x%p=b
6+
if (a % p == 0 && b)
7+
{
8+
return -1;
9+
}
10+
if (b == 1)
11+
{
12+
return 0;
13+
}
14+
map<ll, ll> h;
15+
ll m = sqrt(p) + 1, s = 1;
16+
for (ll i = 1; i <= m; ++i)
17+
{
18+
h[b] = i;
19+
s = s * a % p, b = b * a % p;
20+
}
21+
a = s;
22+
for (ll i = 1; i <= m; ++i)
23+
{
24+
if (h.count(a))
25+
{
26+
return i * m - h[a] + 1;
27+
}
28+
a = a * s % p;
29+
}
30+
return -1;
31+
}
32+
signed main()
33+
{
34+
ll a, b, p;
35+
cin >> p >> a >> b;
36+
ll ans = bsgs(a, b, p);
37+
cout << (ans == -1 ? "no solution" : to_string(ans));
38+
return 0;
39+
}

code/p4195_exbsgs2.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define LL long long
4+
int read()
5+
{
6+
register int x = 0;
7+
register int y = 1;
8+
register char c = getchar();
9+
while (c < '0' || c > '9')
10+
{
11+
if (c == '-')
12+
y = 0;
13+
c = getchar();
14+
}
15+
while (c >= '0' && c <= '9')
16+
{
17+
x = x * 10 + (c ^ 48);
18+
c = getchar();
19+
}
20+
return y ? x : -x;
21+
}
22+
unordered_map<int, int> mp;
23+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
24+
int BSGS(int a, int n, int p, int ad)
25+
{
26+
mp.clear();
27+
int m = ceil(sqrt(p));
28+
int s = 1;
29+
for (int i = 0; i < m; i++, s = 1ll * s * a % p)
30+
mp[1ll * s * n % p] = i;
31+
for (int i = 0, tmp = s, s = ad; i <= m; i++, s = 1ll * s * tmp % p)
32+
if (mp.find(s) != mp.end())
33+
if (1ll * i * m - mp[s] >= 0)
34+
return 1ll * i * m - mp[s];
35+
return -1;
36+
}
37+
int exBSGS(int a, int n, int p)
38+
{
39+
a %= p;
40+
n %= p;
41+
if (n == 1 || p == 1)
42+
return 0;
43+
int cnt = 0;
44+
int d, ad = 1;
45+
while ((d = gcd(a, p)) ^ 1)
46+
{
47+
if (n % d)
48+
return -1;
49+
cnt++;
50+
n /= d;
51+
p /= d;
52+
ad = (1ll * ad * a / d) % p;
53+
if (ad == n)
54+
return cnt;
55+
}
56+
LL ans = BSGS(a, n, p, ad);
57+
if (ans == -1)
58+
return -1;
59+
return ans + cnt;
60+
}
61+
signed main()
62+
{
63+
int a = read(), p = read(), n = read();
64+
while (a || p || n)
65+
{
66+
int ans = exBSGS(a, n, p);
67+
if (~ans)
68+
printf("%d\n", ans);
69+
else
70+
puts("No Solution");
71+
a = read();
72+
p = read();
73+
n = read();
74+
}
75+
return 0;
76+
}

0 commit comments

Comments
 (0)