有点闲,于是再来恶搞一发。
1. 正解
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
递推解法
#include<bits/stdc++.h>
using namespace std;
int f[3];
int main()
{
int a, b;
cin >> a >> b;
f[1] = a;
f[2] = b;
for(int i = 3; i <= 3; i ++ ) f[i] = f[i - 1] + f[i - 2];
cout << f[3] << endl;
return 0;
}
矩阵乘法
矩阵乘法求fib数列。
#include<bits/stdc++.h>
using namespace std;
int q, b;
int a[2][2] =
{
{0, 1},
{1, 1}
};
int n;
void mo(int f[])
{
int ans[2] = {0};
for(int i = 0; i < 2; i ++)
{
for(int j = 0; j < 2; j ++)
{
ans[i] += f[j] * a[i][j];
}
}
for(int i = 0; i < 2; i ++) f[i] = ans[i];
}
int main()
{
cin >> q >> b;
int f[3] = {q, b};
for(int i = 1; i <= 1; i ++) mo(f);
cout << f[1];
return 0;
}
最短路之Dijkstra
1-2建一条a的边,2-3建一条b的边。
所以的话1-3的最短路就是答案。
#include<bits/stdc++.h>
using namespace std;
const int N = 200020;
int n, m, h[N], e[N], w[N], ne[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int dijkstra()
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
heap.push({0, 1});
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
while(!heap.empty())
{
auto t = heap.top();
heap.pop();
int vel = t.second;
if(st[vel]) continue;
st[vel] = true;
for(int i = h[vel]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[vel] + w[i])
{
dist[j] = dist[vel] + w[i];
heap.push({dist[j], j});
}
}
}
if(dist[3] == 0x3f3f3f3f) return -1;
return dist[3];
}
int main()
{
int a, b;
memset(h, -1, sizeof h);
cin >> a >> b;
add(1, 2, a);
add(2, 3, b);
int t = dijkstra();
cout << t;
return 0;
}
最短路之spfa
老样子。
#include<bits/stdc++.h>
using namespace std;
const int N = 200020;
int n, m, h[N], e[N], w[N], ne[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
queue<int> q;
q.push(1);
st[1] = true;
dist[1] = 0;
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
if(dist[3] == 0x3f3f3f3f) return -1;
return dist[3];
}
int main()
{
int a, b;
memset(h, -1, sizeof h);
cin >> a >> b;
add(1, 2, a);
add(2, 3, b);
int t = spfa();
cout << t;
return 0;
}
# %%%NB
大牛666呀
这题的正解是主席树
#include[HTML_REMOVED]
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
这是什么鬼
# %%%
大佬太厉害了!
秀啊
(卑微的我最多只能打出BFS代码)
以后就都会了
你是真的秀hh
哈哈
可惜了不能投币
qaq