9.19 考试总结
35分爆炸记
很久没有这么低过了
第一题矩阵乘法模板,但我忘了(你好意思吗)
照着lyd的代码抄了一下(话说他的代码是真的丑)
结果只得了\(15pts\),后来调了一下午才搞清楚应该是从\(f[1]\)开始推(而不是\(f[0]\))
第二题暴搜找规律都能过,但我第一眼感觉很难,于是刚第三题
第三题看完题就知道大概是一个边双缩点+树形\(dp\)
(但边双模板又双叒叕忘了)
好久都没复习了,md今晚上爆肝看
于是我又翻出了lyd的模板,好不容易打出来了,结果出成绩只过了一个点(\(20pts\)),我已经开始怀疑人生了
后来调了一会儿,先发现边的数组开小了,改了一交有\(40pts\),后来再看发现树形\(dp\)的往上走的数组推错了,我写成了自底向上(应该是自顶向下),跑了两遍\(dfs\)就过了
总的来说是巨大的失误,绝对不能再有下一次了
模板一定要熟!
模板一定要熟!
模板一定要熟!
重要的事说三遍
\(t1\) 代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <typename T> void in(T &x) {
x = 0; T f = 1; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while( isdigit(ch)) {x = 10*x+ch-'0'; ch = getchar();}
x *= f;
}
template <typename T> void out(T x) {
if(x < 0) putchar('-'),x = -x;
if(x > 9) out(x/10);
putchar(x%10+'0');
}
//---------------------------------------------------------------
const int mod = 7;
ll A,B,n;
struct Mat {
int r,c;
ll A[2][2];
Mat() {r = c = 0; memset(A,0,sizeof(A));}
Mat(int x) {
r = c = x; memset(A,0,sizeof(A));
for(int i = 0;i < x; ++i) A[i][i] = 1;
}
Mat operator * (const Mat &sed) const {
Mat res; res.r = r,res.c = sed.c;
for(int i = 0;i < 2; ++i) {
for(int j = 0;j < 2; ++j) {
for(int k = 0;k < 2; ++k) {
res.A[i][j] = (res.A[i][j]+(A[i][k]*sed.A[k][j])%mod)%mod;
}
}
}
return res;
}
}a,ans;
Mat power(Mat a,ll b) {
Mat res(a.r);
for(;b;b >>= 1) {
if(b&1) res = res*a;
a = a*a;
}
return res;
}
int main() {
in(A); in(B); in(n);
if(n <= 2) {
out(1); return 0;
}
a.r = a.c = 2;
a.A[0][0] = A; a.A[0][1] = 1;
a.A[1][0] = B; a.A[1][1] = 0;
ans.r = 1,ans.c = 2;
ans.A[0][0] = ans.A[0][1] = 1;
ans = ans*power(a,n-2);
out((ans.A[0][0])%mod);
return 0;
}
\(t2\) \(dfs\) 代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <typename T> void in(T &x) {
x = 0; T f = 1; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while( isdigit(ch)) {x = 10*x+ch-'0'; ch = getchar();}
x *= f;
}
template <typename T> void out(T x) {
if(x < 0) putchar('-'),x = -x;
if(x > 9) out(x/10);
putchar(x%10+'0');
}
//---------------------------------------------------------------
int n,m,ans;
int x,y;
int fact[] = {0,1,2,6,24,120,720,5040,40320,362880,3628800};
int gcd(int a,int b) {
return b == 0 ? a : gcd(b,a%b);
}
int get(int x) {
int res = 0;
for(;x;x >>= 1) res += (x&1);
return res;
}
void dfs(int k,int s1,int s2) {
int c1 = get(s1),c2 = get(s2);
if(c2 > c1) return;
if(k == n+m+1) {
++ans; return;
}
for(int i = 0;i < n; ++i) if(!(s1&(1<<i))) dfs(k+1,s1|(1<<i),s2);
for(int i = 0;i < m; ++i) if(!(s2&(1<<i))) dfs(k+1,s1,s2|(1<<i));
}
void simply(int a1,int a2) {
while(gcd(a1,a2) != 1) {
int tmp1 = a1/gcd(a1,a2),tmp2 = a2/gcd(a1,a2);
a1 = tmp1,a2 = tmp2;
}
x = a1,y = a2;
}
int main() {
for(n = 1;n <= 5; ++n) {
for(m = 1;m <= 5; ++m) {
ans = 0;
dfs(1,0,0);
if(ans == 0) {
printf("n:%d m:%d 0\n",n,m); continue;
}
simply(ans,fact[n+m]);
printf("n:%d m:%d %d/%d\n",n,m,x,y);
}
cout << endl;
}
return 0;
}
\(t2\) 正解
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <typename T> void in(T &x) {
x = 0; T f = 1; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while( isdigit(ch)) {x = 10*x+ch-'0'; ch = getchar();}
x *= f;
}
template <typename T> void out(T x) {
if(x < 0) putchar('-'),x = -x;
if(x > 9) out(x/10);
putchar(x%10+'0');
}
//---------------------------------------------------------------
int T,n,m;
int main() {
in(T);
while(T--) {
in(n); in(m);
if(m > n) {
printf("%.6lf\n",0.0); continue;
}
printf("%.6lf\n",(double)(n-m+1)/(n+1));
}
return 0;
}
\(t3\) 代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#define ll long long
using namespace std;
template <typename T> void in(T &x) {
x = 0; T f = 1; char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while( isdigit(ch)) {x = 10*x+ch-'0'; ch = getchar();}
x *= f;
}
template <typename T> void out(T x) {
if(x < 0) putchar('-'),x = -x;
if(x > 9) out(x/10);
putchar(x%10+'0');
}
//---------------------------------------------------------------
const int N = 20005,M = 200005;
int head[N],ver[M<<1],w[M<<1],nxt[M<<1];
int dfn[N],low[N],n,m,tot,num;
bool bridge[M<<1];
int c[N],dcc;
struct edge {
int v,w,nxt;
edge(int v = 0,int w = 0,int nxt = 0):v(v),w(w),nxt(nxt){}
}e[M<<1]; int hc[N],tc;
void add_c(int u,int v,int _w) {
e[++tc] = edge(v,_w,hc[u]); hc[u] = tc;
}
void add(int u,int v,int _w) {
ver[++tot] = v,w[tot] = _w; nxt[tot] = head[u],head[u] = tot;
}
void tarjan(int u,int in_e) {
dfn[u] = low[u] = ++num;
for(int i = head[u]; i;i = nxt[i]) {
int v = ver[i];
if(!dfn[v]) {
tarjan(v,i); low[u] = min(low[u],low[v]);
if(low[v] > dfn[u]) bridge[i] = bridge[i^1] = 1;
}
else if(i != (in_e^1)) low[u] = min(low[u],dfn[v]);
}
}
void dfs(int u) {
c[u] = dcc;
for(int i = head[u]; i;i = nxt[i]) {
int v = ver[i];
if(c[v] || bridge[i]) continue;
dfs(v);
}
}
ll d[N],f[N];
void dp1(int u,int fa) {
d[u] = 0;
for(int i = hc[u]; i;i = e[i].nxt) {
int v = e[i].v; if(v == fa) continue;
dp1(v,u);
d[u] = max(d[u],d[v]+e[i].w);
}
}
void dp2(int u,int fa) {
ll maxd1 = 0,id1,maxd2 = 0;
for(int i = hc[u]; i;i = e[i].nxt) {
int v = e[i].v; if(v == fa) continue;
ll tmp = d[v]+e[i].w;
if(tmp >= maxd1) maxd2 = maxd1,maxd1 = tmp,id1 = v; //>=
else if(tmp > maxd2) maxd2 = tmp;
}
for(int i = hc[u]; i;i = e[i].nxt) {
int v = e[i].v; if(v == fa) continue;
if(v != id1) f[v] = e[i].w + max(f[u],maxd1);
else f[v] = e[i].w + max(f[u],maxd2);
dp2(v,u);
}
}
int main() {
in(n); in(m);
int i,u,v,_w; tot = 1;
for(i = 1;i <= m; ++i) {
in(u); in(v); in(_w);
add(u,v,_w); add(v,u,_w);
}
for(int i = 1;i <= n; ++i) {
if(!dfn[i]) tarjan(i,0);
}
for(int i = 1;i <= n; ++i) {
if(!c[i]) {
++dcc;
dfs(i);
}
}
tc = 1;
for(int i = 2;i <= tot; ++i) {
int u = ver[i^1],v = ver[i];
if(c[u] == c[v]) continue;
add_c(c[u],c[v],w[i]);
}
dp1(1,0);
dp2(1,0);
for(int i = 1;i <= n; ++i) {
out(max(f[c[i]],d[c[i]])); putchar('\n');
}
return 0;
}