小白月赛8
C-神秘钥匙:题目链接
这个题是一个规律水题,公式为n*2^(n-1),然后再用快速幂计算
#include<cstdlib>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e4;
typedef long long ll;
ll n;
ll mod = 1000000007;
ll quickpow(ll x, ll n, ll mod)
{
ll res = 1;
while (n > 0) {
if (n % 2 == 1) {
res = res*x;
res = res%mod;
}
x = x*x; //x乘以二
x = x%mod;
n >>= 1; //n除以
}
return res;
}
int main()
{
while(cin >> n)
cout << n*quickpow(2, n - 1, mod)%mod << endl;
return 0;
}
F-数列操作:题目链接
这个题的正经题解是平衡树,然而我太弱了。。。不会写。然后在赛后查看了大佬们的代码,发现用vector可以暴力暴过去,所以下面附上的是vector代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
vector<int>vec;
int num,x;
int n;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++){
cin>>num>>x;
if(num==1){
vec.insert(lower_bound(vec.begin(),vec.end(),x),x);
}
else if(num==2){
vec.erase(lower_bound(vec.begin(),vec.end(),x));
}
else if(num==3){
cout<<lower_bound(vec.begin(),vec.end(),x)-vec.begin()+1<<endl;
}
else if(num==4){
cout<<vec[x-1]<<endl;
}
else if(num==5){
cout<<*(lower_bound(vec.begin(),vec.end(),x)-1)<<endl;
}
else{
cout<<*upper_bound(vec.begin(), vec.end(), x)<<endl;
}
}
return 0;
}
小白月赛9
A-签到:题目链接
小白月赛不小白啊QAQ。。。
A题算法就是求一个乘法逆元,下附的代码是用扩展欧几里得写的
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
ll a, b;ll p = 1;ll q = 1;ll n;
ll exgcd(ll r, ll s, ll &x, ll &y)
{
if (s == 0) {
x = 1; y = 0;
return r;
}
ll xx, yy;
ll g = exgcd(s, r%s, xx, yy);
x = yy;
y = xx - (r / s)*yy;
return g;
}
ll inverse(ll r, ll s)
{
ll x, y;
exgcd(r, s, x, y);
return (x%s + s) % s;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a >> b;
p = ((b - a)*p) % mod;
q = (b*q) % mod;
}
if (q < p)q = q + mod;
ll zi = q - p, mu = q%mod;
cout << (zi * (inverse(mu, mod))) % mod << endl;
return 0;
}
小白月赛10
D-饥饿:题目链接
一个最短路的模板题,我是用Dijsktra+堆优化+前向星建图写的
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<cstring>
#include<queue>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 500000;
const int inf = 0x3f3f3f3f;
struct Node {
ll v;
ll w;
ll nex;
}edge[maxn];
ll head[maxn];
ll dis[maxn];
bool vis[maxn];
ll n, m, s, t, cnt;
void init() {
memset(head, -1, sizeof(head));
cnt = 0;
}
void add(ll u, ll v, ll w) {
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].nex = head[u];
head[u] = cnt++;
}
struct dui //堆优化
{
int pos, dis; //点的位置及距离
dui() {}
dui(int p, int d){
pos = p;
dis = d;
}
bool operator < (const dui &x)const{//重载小于号
return dis>x.dis;
}
};
void Dijkstra(ll start) {
priority_queue<dui>Q;
fill(dis, dis + 1 + n, inf);
memset(vis, 0, sizeof(vis));
dis[start] = 0;
Q.push(dui(start,0));
while (!Q.empty())
{
ll now = Q.top().pos;
Q.pop();
int u = now;
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -1; i = edge[i].nex) {
int v = edge[i].v;
if (dis[v] > dis[u] + edge[i].w) {
dis[v] = dis[u] + edge[i].w;
Q.push(dui(v, dis[v]));
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m >> s >> t;
init();
ll u, v, w;
int f;
for (int i = 0; i < m; i++) {
cin >> f >> u >> v >> w;
if (f == 0)continue;
add(u, v, w);
add(v, u, w);
}
Dijkstra(s);
if (dis[t] >= inf) {
cout << "My gold!!!" << endl;
return 0;
}
cout << dis[t] << endl;
return 0;
}