Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)

本文解析了五道涉及等比求和、树的破坏、矩形切割、字符串频率及等待圆圈的编程题目,提供了完整的代码实现及思路分析。

A.Alternating Sum

等比求和

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1e5 + 10;
 5 const LL mod = 1e9 + 9;
 6 char s[maxn];
 7 
 8 LL qpow(LL a, LL b)
 9 {
10     LL ret = 1LL;
11     while(b)
12     {
13         if(b & 1) ret = ret * a % mod;
14         a = a * a % mod;
15         b >>= 1;
16     }
17     return ret;
18 }
19 
20 LL inv(LL x)
21 {
22     return qpow(x, mod - 2);
23 }
24 
25 int main(){
26     LL n, a, b, k;
27     scanf("%I64d %I64d %I64d %I64d %s", &n, &a, &b, &k, s);
28     LL base = qpow(a, n), sum = 0, r = (n + 1) / k, ans;
29     for(int i = 0; i < k; ++i){
30         if(s[i] == '+') sum = (sum + base) % mod;
31         else sum = (sum - base + mod) % mod;
32         base = base * b % mod * inv(a) % mod;
33     }
34     LL tmp = qpow(b, k) * inv(qpow(a, k)) % mod;
35     if(tmp == 1) ans = r * sum % mod;
36     else ans = sum * (1 - qpow(tmp, r) + mod) % mod * inv((1 - tmp + mod) % mod) % mod;
37     printf("%I64d\n", ans);
38     return 0;
39 }
Aguin

 

B.Destruction of a Tree

大家的贪心水平都很高阿

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2e5 + 10;
 4 vector<int> G[maxn], ans;
 5 
 6 vector<int> son[maxn][2][2]; // d/nd y/n
 7 int df[maxn], ndf[maxn];
 8 void dfs1(int x, int f){
 9     for(int i = 0; i < G[x].size(); ++i){
10         int to = G[x][i];
11         if(to == f) continue;
12         dfs1(to, x);
13         son[x][df[to]][ndf[to]].push_back(to);
14     }
15     if(son[x][0][0].size() || son[x][1][0].size() % 2 == 1 && son[x][1][1].size() == 0) df[x] = 0;
16     else df[x] = 1;
17     if(son[x][0][0].size() || son[x][1][0].size() % 2 == 0 && son[x][1][1].size() == 0) ndf[x] = 0;
18     else ndf[x] = 1;
19 }
20 
21 void dfs2(int x, int del_f){
22     int cnt = 0;
23     for(int i = 0; i < son[x][0][1].size(); ++i){
24         dfs2(son[x][0][1][i], 0);
25     }
26     if(del_f && son[x][1][0].size() % 2 == 1 || !del_f && son[x][1][0].size() % 2 == 0){
27         int tmp = son[x][1][1][son[x][1][1].size()-1];
28         son[x][1][1].pop_back();
29         dfs2(tmp, 0);
30     }
31     ans.push_back(x);
32     for(int i = 0; i < son[x][1][1].size(); ++i){
33         dfs2(son[x][1][1][i], 1);
34     }
35     for(int i = 0; i < son[x][1][0].size(); ++i){
36         dfs2(son[x][1][0][i], 1);
37     }
38 }
39 
40 int main(){
41     int n;
42     scanf("%d", &n);
43     for(int i = 1; i <= n; ++i){
44         int p;
45         scanf("%d", &p);
46         if(p) G[i].push_back(p), G[p].push_back(i);
47     }
48     dfs1(1, 0);
49     if(df[1] == 0) puts("NO");
50     else{
51         dfs2(1, 1);
52         puts("YES");
53         for(int i = 0; i < n; ++i) printf("%d\n", ans[i]);
54     }
55     return 0;
56 }
Aguin

 

C.Cutting Rectangle

随便枚举

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 2e5 + 10;
 5 LL w[maxn], h[maxn], c[maxn];
 6 map< LL, vector<int> > M;
 7 map< LL, LL > G;
 8 map< LL, vector<int> > :: iterator it, itt;
 9 
10 bool cmp(int i, int j){
11     return h[i] < h[j];
12 }
13 
14 LL gcd(LL a, LL b){
15     return a % b ? gcd(b, a % b) : b;
16 }
17 
18 int main(){
19     int n;
20     scanf("%d", &n);
21     for(int i = 1; i <= n; ++i) {
22         cin >> w[i] >> h[i] >> c[i];
23         M[w[i]].push_back(i);
24     }
25     LL m = 1e18;
26     for(it = M.begin(); it != M.end(); ++it){
27         LL W = (*it).first;
28         vector<int> & t = (*it).second;
29         sort(t.begin(), t.end(), cmp);
30         G[W] = c[t[0]];
31         for(int j = 0; j < t.size(); ++j) G[W] = gcd(G[W], c[t[j]]);
32         LL sum = 0;
33         for(int j = 0; j < t.size(); ++j) sum += c[t[j]] / G[W];
34         if(sum < m) m = sum, itt = it;
35     }
36     LL ok = 1, g = G[(*itt).first], ans = 0;
37     for(it = M.begin(); it != M.end(); ++it){
38         vector<int> & t = (*it).second;
39         vector<int> & tt = (*itt).second;
40         LL W = (*itt).first;
41         if(t.size() != tt.size()) {ok = 0; break;}
42         for(int j = 0; j < t.size(); ++j){
43             if(h[t[j]] != h[tt[j]]) ok = 0;
44             if(c[t[j]] % (c[tt[j]] / G[W]) != 0) ok = 0;
45             if(c[t[j]] / (c[tt[j]] / G[W]) != c[t[0]] / (c[tt[0]] / G[W])) ok = 0;
46         }
47         if(!ok) break;
48         g = gcd(g, c[t[0]] / (c[tt[0]] / G[W]));
49     }
50     for(LL i = 1; i * i <= g; ++i){
51         if(g % i == 0){
52             ans += 2;
53             if(i * i == g) ans--;
54         }
55     }
56     cout << ok * ans << endl;
57     return 0;
58 }
Aguin

 

D.Frequency of String

蛤习天下无敌

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 1e5 + 10;
 5 const int INF = 1e9;
 6 char s[maxn], T[maxn];
 7 vector<int> pos[maxn], id[maxn];
 8 int k[maxn], ans[maxn];
 9 
10 // Hash
11 const int seed = 131;
12 LL mod = 1000000007;
13 LL po[maxn], P[maxn], PT[maxn], t0[maxn];
14 
15 void init(int len)
16 {
17     P[0] = 0, po[0] = 1;
18     for(int i = 1; i <= len; i++)
19         P[i] = (P[i-1] * seed + s[i]) % mod, po[i] = po[i-1] * seed % mod;
20 }
21 map<LL, int> M;
22 
23 int main(){
24     scanf("%s", s + 1);
25     int len = strlen(s + 1);
26     init(len);
27     int n;
28     scanf("%d", &n);
29     for(int kase = 1; kase <= n; ++kase) {
30         scanf("%d%s", k + kase, T + 1);
31         int lt = strlen(T + 1);
32         for (int t = 0; t <= 0; t++) {
33             PT[0] = 0;
34             for (int i = 1; i <= lt; i++)
35                 PT[i] = (PT[i - 1] * seed + T[i]) % mod;
36         }
37         t0[kase] = PT[lt];
38         ans[kase] = INF;
39         id[lt].push_back(kase);
40     }
41     for(int i = 1; i <= len; ++i) {
42         if(id[i].size() == 0) continue;
43         M.clear();
44         for(int j = 0; j < id[i].size(); ++j){
45             int x = id[i][j];
46             M[t0[x]] = x;
47         }
48         for(int j = i; j <= len; ++j){
49             LL T0 = (P[j] - P[j-i] * po[i] % mod + mod) % mod;
50             if(M.find(T0) == M.end()) continue;
51             int x = M[T0];
52             pos[x].push_back(j);
53             if(pos[x].size() >= k[x]) ans[x] = min(pos[x][pos[x].size() - 1] - pos[x][pos[x].size() - k[x]] + i, ans[x]);
54         }
55     }
56     for(int i = 1; i <= n; ++i)
57         printf("%d\n", ans[i] == INF ? -1 : ans[i]);
58     return 0;
59 }
Aguin

 

E.Circles of Waiting

稀疏矩阵求个逆

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const LL mod = 1e9 + 7;
 5 typedef pair<int, int> pii;
 6 int sqr(int x){return x * x;}
 7 vector<pii> v;
 8 map<pii, int> id;
 9 int m[8000][8000], X[8000];
10 int dx[] = {1, 0, -1, 0};
11 int dy[] = {0, 1, 0, -1};
12 int cnt;
13 
14 LL qpow(LL a, LL b) {
15     LL ret = 1LL;
16     while(b) {
17         if(b & 1) ret = ret * a % mod;
18         a = a * a % mod;
19         b >>= 1;
20     }
21     return ret;
22 }
23 
24 LL inv(LL x) {
25     return qpow(x, mod - 2);
26 }
27 
28 void CAL(){
29     for(int i = 0; i < cnt; ++i){
30         LL x = inv(m[i][i]);
31         for(int j = i; j < min(cnt, i + 111); ++j) m[i][j] = m[i][j] * x % mod;
32         X[i] = X[i] * x % mod;
33         vector<int> O;
34         vector<LL> T;
35         for(int j = i + 1; j < min(i + 111, cnt); ++j){
36             if(j == i) continue;
37             if(!m[j][i]) continue;
38             O.push_back(j), T.push_back(m[j][i]);
39             X[j] = (X[j] - (LL) X[i] * m[j][i] % mod + mod) % mod;
40         }
41         for(int j = i; j < min(cnt, i + 111); ++j){
42             if(!m[i][j]) continue;
43             for(int k = 0; k < O.size(); ++k) m[O[k]][j] = (m[O[k]][j] - T[k] * m[i][j] % mod + mod) % mod;
44         }
45     }
46     for(int i = cnt - 1; i >= 0; --i) {
47         for (int j = i - 1; j >= max(0, i - 111); --j) {
48             if (m[j][i] == 0) continue;
49             X[j] = (X[j] - (LL) X[i] * m[j][i] % mod + mod) % mod;
50         }
51     }
52 }
53 
54 int main(){
55     int R, a[4];
56     scanf("%d", &R);
57     for(int i = 0; i < 4; ++i) scanf("%d", a + i);
58 
59     for(int i = 0; i <= 200; ++i){
60         for(int j = 0; j <= 200; ++j){
61             if(sqr(100 - i) + sqr(100 - j) <= sqr(R)) v.push_back(pii(i, j)), id[pii(i, j)] = cnt++;
62         }
63     }
64 
65     for(int i = 0; i < cnt; ++i){
66         m[i][i] = 1;
67         for(int k = 0; k < 4; ++k){
68             int xx = v[i].first + dx[k];
69             int yy = v[i].second + dy[k];
70             if(id.find(pii(xx, yy)) == id.end()) continue;
71             m[i][id[pii(xx, yy)]] = (mod - a[k] * inv(a[0] + a[1] + a[2] + a[3]) % mod + mod) % mod;
72         }
73     }
74 
75     X[id[pii(100, 100)]] = 1;
76     CAL();
77 
78     LL ans = 0;
79     for(int i = 0; i < cnt; ++i) ans = (ans + X[i]) % mod;
80     cout << ans << endl;
81 
82     return 0;
83 }
Aguin

 

转载于:https://www.cnblogs.com/Aguin/p/8872451.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值