其他分享
首页 > 其他分享> > CSP-S开小灶4

CSP-S开小灶4

作者:互联网

A. 山洞

暴力枚举 \(nm\) 暴力

然后发现枚举的步长每 \(n\) 个一循环

搞出从\(0, 0\) 走 \(n\) 步的数组

进而发现从 \(0\) 到 \(i\) 和从 \(x\) 到 \((x + i) \% n\) 等价

于是可以\(n^2\) 转移出走 \(n + n\) 步,然后发现可以倍增了

其实这本质上是个循环矩阵

我太菜了,今天才知道啥是循环矩阵

code
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1005;
const int mod = 1e9 + 7;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int f[2][maxn], b[21][maxn], ans[maxn], tmp[maxn];
int n, m;
int main(){
	n = read(); m = read();
	f[0][0] = 1;
	int zt = 1;
	for(int i = 1; i <= min(n, m); ++i, zt = 1 - zt)
		for(int p = 0; p < n; ++p)
			if(f[1 - zt][p]){
				int p1 = (p + i) % n, p2 = ((p - i) % n + n) % n;
				f[zt][p1] = (f[zt][p1] + f[1 - zt][p]) % mod;
				if(p1 != p2)f[zt][p2] = (f[zt][p2] + f[1 - zt][p]) % mod;
				f[1 - zt][p] = 0;
			}
	if(m <= n){
		printf("%d\n",f[m & 1][0]);
		return 0;
	}
	for(int i = 0; i < n; ++i)b[0][i] = f[n & 1][i];
	int round = m / n; ans[0] = 1;
	if(round & 1)for(int i = 0; i < n; ++i)ans[i] = b[0][i];
	for(int j = 1; (1 << j) <= round; ++j){
		for(int i = 0; i < n; ++i)if(b[j - 1][i]){
			for(int k = 0; k < n; ++k){
				b[j][(i + k) % n] = (b[j][(i + k) % n] + 1ll * b[j - 1][i] * b[j - 1][k] % mod) % mod;
			}
		}
		if(round & (1 << j)){
			for(int i = 0; i < n; ++i)
				for(int k = 0; k < n; ++k){
					tmp[(i + k) % n] = (tmp[(i + k) % n] + 1ll * ans[i] * b[j][k] % mod) % mod;
				}
			for(int i = 0; i < n; ++i)ans[i] = tmp[i];
			for(int i = 0; i < n; ++i)tmp[i] = 0;
		}
	}
	int res = m % n;
	for(int i = 1; i <= res; ++i){
		for(int p = 0; p < n; ++p)if(ans[p]){
			int p1 = (p + i) % n, p2 = ((p - i) % n + n) % n;
			tmp[p1] = (tmp[p1] + ans[p]) % mod;
			if(p1 != p2)tmp[p2] = (tmp[p2] + ans[p]) % mod;
			ans[p] = 0;
		}
		for(int p = 0; p < n; ++p)ans[p] = tmp[p];
		for(int p = 0; p < n; ++p)tmp[p] = 0;
	}
	printf("%d\n",ans[0]);
	return 0;
}

B. beauty

对每条边的贡献分开考虑

\(\sum_{i = 1}^{n - 1} min(cnt_i, k +k - cnt_i)\)

感觉最近想 \(DP\) 有点魔怔了

code
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100005;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int n, k;
int key[maxn];
vector<int>g[maxn];
int ans;
void dfs(int x, int fa){
	for(int v : g[x]){
		if(v == fa)continue;
		dfs(v, x);
		ans = (ans + min(key[v], k + k - key[v]));
		key[x] += key[v];
	}
}
int main(){
	n = read(), k = read(); read();
	for(int i = 1; i <= k + k; ++i)key[read()] = 1;
	for(int i = 1; i < n; ++i){
		int u = read(), v = read();
		g[u].push_back(v); g[v].push_back(u);
	}
	dfs(1, 0);
	printf("%d\n",ans);
	return 0;
}

标签:int,long,read,maxn,key,ans,开小灶,CSP
来源: https://www.cnblogs.com/Chencgy/p/16698066.html