BJOI2018
大佬:这些题都太水啦,随随便便就AK啦
我:BJ考的都是些什么神仙题啊???
听说是结论题
我推了一个结论然后YY了一个线段树做法打了200+弃疗了打了个暴力,最后发现结论有点点问题还好只打了暴力?
怎么写啊,我不会.
听说又是结论题
我不会呀
到底有什么结论呀..
最后来一道lca裸题送温暖吗,可惜我没领出题人这个情,预处理从2开始,然后GG了.
1 //Achen
2 #include<algorithm>
3 #include<iostream>
4 #include<cstring>
5 #include<cstdlib>
6 #include<vector>
7 #include<cstdio>
8 #include<queue>
9 #include<cmath>
10 #include<set>
11 #define For(i,a,b) for(int i=(a);i<=(b);i++)
12 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
13 const int mod=998244353,N=300007;
14 typedef long long LL;
15 typedef double db;
16 using namespace std;
17 int n,m;
18 LL kf[N][51],pr[N][51];
19
20 template<typename T> void read(T &x) {
21 char ch=getchar(); x=0; T f=1;
22 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
23 if(ch=='-') f=-1,ch=getchar();
24 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
25 }
26
27 int ecnt,fir[N],nxt[N<<1],to[N<<1];
28 void add(int u,int v) {
29 nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v;
30 nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u;
31 }
32
33 int R[N],f[N][20];
34 void dfs(int x,int fa) {
35 f[x][0]=fa;
36 For(i,1,18) f[x][i]=f[f[x][i-1]][i-1];
37 for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
38 R[to[i]]=R[x]+1;
39 dfs(to[i],x);
40 }
41 }
42
43 int lca(int x,int y) {
44 if(R[x]<R[y]) swap(x,y);
45 Rep(i,18,0) if(R[f[x][i]]>=R[y])
46 x=f[x][i];
47 if(x==y) return x;
48 Rep(i,18,0) if(f[x][i]!=f[y][i])
49 x=f[x][i],y=f[y][i];
50 return f[x][0];
51 }
52
53 //#define DEBUG
54 int main() {
55 #ifdef DEBUG
56 freopen("sum.in","r",stdin);
57 freopen("sum.out","w",stdout);
58 #endif
59 read(n);
60 For(i,1,n-1) {
61 int x,y;
62 read(x); read(y);
63 add(x,y);
64 }
65 dfs(1,0);
66 For(i,1,n) kf[i][0]=1;
67 For(i,1,n) For(j,1,50) {
68 kf[i][j]=kf[i][j-1]*i%mod;
69 pr[i][j]=(pr[i-1][j]+kf[i][j])%mod;
70 }
71 read(m);
72 For(i,1,m) {
73 int x,y,k;
74 read(x); read(y); read(k);
75 int z=lca(x,y),w=z; z=f[z][0];
76 LL ans=((pr[R[x]][k]+pr[R[y]][k])%mod-2LL*pr[R[z]][k]%mod+mod)%mod;
77 ans=(ans-kf[R[w]][k]+mod)%mod;
78 printf("%lld\n",ans);
79 }
80 return 0;
81 }
82 /*
83 5
84 1 2
85 1 3
86 2 4
87 2 5
88 2
89 1 4 5
90 5 4 45
91 */
真有意思
可以手玩两个小数据
我不会呀
这道题难得找得到题解
但是我不想看
等sxyA了给我讲
考前一天刚好被llj讲到
然而我和sxy都因为没有判-1爆成10分
列出柿子高斯消元
发现矩阵很有特点,先O(n)地消成1~n-1行的i,i+1有元素,n行的n有元素,再O(n)地消成对角线即可.
我常数太大,被洛谷卡了
llj也被洛谷卡了,但是本地过了,老张的机子真快
sxy用更优秀的写法成功bzoj 和luogu都是rank1,但是感觉本质是一样的,这也算常数优化吗?总之Orz orz
1 //Achen
2 #include<algorithm>
3 #include<iostream>
4 #include<cstring>
5 #include<cstdlib>
6 #include<vector>
7 #include<cstdio>
8 #include<queue>
9 #include<cmath>
10 #include<set>
11 #define For(i,a,b) for(register int i=(a);i<=(b);i++)
12 #define Rep(i,a,b) for(register int i=(a);i>=(b);i--)
13 const int mod=1000000007;
14 typedef long long LL;
15 typedef double db;
16 using namespace std;
17 LL T,n,p,m,k;
18 LL p1,p2,C[1555],inv[1555],power1[1555],power2[1555];
19
20 template<typename T> void read(T &x) {
21 char ch=getchar(); x=0; T f=1;
22 while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
23 if(ch=='-') f=-1,ch=getchar();
24 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
25 }
26
27 inline LL ksm(LL a,LL b) {
28 LL rs=1,bs=a;
29 while(b) {
30 if(b&1) rs=rs*bs%mod;
31 bs=bs*bs%mod;
32 b>>=1;
33 }
34 return rs;
35 }
36
37 void mo(LL &x) { if(x>=mod) x-=mod; }
38
39 LL g[1555][1555];
40 inline int solve(int n) {
41 For(i,1,n) {
42 LL tp=ksm(g[i][i],mod-2);
43 if(g[i][i]==0) return -1;
44 LL A=g[i][n+1]*tp%mod;
45 LL B=(mod-g[i][i+1])*tp%mod;
46 For(j,i+1,n) {
47 mo(g[j][i+1]+=B*g[j][i]%mod);
48 mo(g[j][n+1]+=(mod-A*g[j][i]%mod));
49 g[j][i]=0;
50 }
51 }
52 Rep(i,n,1) {
53 For(j,i+1,n) {
54 mo(g[i][n+1]+=(mod-g[j][n+1]*g[i][j]%mod));
55 }
56 (g[i][n+1]*=ksm(g[i][i],mod-2))%=mod;
57 }
58 return 1;
59 }
60
61 //#define DEBUG
62 int main() {
63 #ifdef DEBUG
64 freopen("heal1.in","r",stdin);
65 //freopen("heal.out","w",stdout);
66 #endif
67 read(T);
68 while(T--) {
69 read(n); read(p); read(m); read(k);
70 C[0]=1; C[1]=k; inv[0]=inv[1]=1;
71 p1=ksm(m+1,mod-2); p2=p1*m%mod;
72 power1[0]=power2[0]=1;
73 power1[1]=p1; power2[1]=p2;
74 For(i,2,n+1) {
75 inv[i]=mod-mod/i*inv[mod%i]%mod;
76 power1[i]=power1[i-1]*p1%mod;
77 power2[i]=power2[i-1]*p2%mod;
78 }
79 For(i,2,n+1) {
80 if(i<=k) C[i]=C[i-1]*max(1LL,(k-i+1))%mod*inv[i]%mod;
81 else C[i]=0;
82 }
83 memset(g,0,sizeof(g));
84 For(i,1,n) {
85 g[i][i]++;
86 LL tpp2;
87 if(i!=n) {
88 tpp2=ksm(p2,k-min((int)k,i));
89 Rep(j,min((int)k,i),0) {
90 LL t=p2*C[j]%mod*power1[j]%mod*tpp2%mod;
91 mo(g[i][i-j]+=mod-t); tpp2=tpp2*p2%mod;
92 }
93 tpp2=ksm(p2,k-min((int)k-1,i)-1);
94 Rep(j,min((int)k-1,i),-1) {
95 LL t=p1*C[j+1]%mod*power1[j+1]%mod*tpp2%mod;
96 mo(g[i][i-j]+=mod-t); tpp2=tpp2*p2%mod;
97 }
98 }
99 else {
100 LL tpp2=ksm(p2,k-min((int)k,i));
101 Rep(j,min((int)k,i),0) {
102 LL t=C[j]*power1[j]%mod*tpp2%mod;
103 mo(g[i][i-j]+=mod-t); tpp2=tpp2*p2%mod;
104 }
105 }
106 g[i][n+1]=1;
107 }
108 int tp=solve(n);
109 if(tp==-1) puts("-1");
110 else printf("%lld\n",g[p][n+1]);
111 }
112 //cerr<<clock()<<endl;
113 return 0;
114 }
115 /*
116 2
117 2 1 1 1
118 2 2 1 1
119
120 2
121 20 15 54 100
122 512 112 255 55665
123 */