当前位置:网站首页>B blocks of the 46th ICPC Asian regional competition (Kunming)
B blocks of the 46th ICPC Asian regional competition (Kunming)
2022-04-23 02:44:00 【JXNU_ SONG】
B Blocks
https://ac.nowcoder.com/acm/contest/32708/B
We first perform binary compression on each rectangle , state i i i Each bit of represents a rectangle , This position is 1 1 1 Indicates that this rectangle has been selected , Otherwise, there is no choice .
Then we set f [ i ] f[i] f[i] Indicates that the current state is i i i , How many more steps do we need to take ( expect ), Before you can put the whole big rectangle W H WH WH fill . that f [ 0 ] f[0] f[0] It's the answer we're asking for .
At this time, we can solve two cases :
(1) state i i i The large rectangle has been W H WH WH fill , So we have :
f [ i ] = 0 f[i]=0 f[i]=0
(2) state i i i The large rectangle has not been W H WH WH fill , Then let's consider the next possible choice at this time , Obviously, we have n n n A choice . that n n n The two options can also be divided into two cases :
① If we choose a rectangle j j j And j j j It's state i i i A rectangle that has been selected ( namely ( i > > j & 1 = 1 ) (i>>j\&1=1) (i>>j&1=1) ), Then the expected number of steps is still f [ i ] f[i] f[i] .
② If the selected rectangle j j j Does not exist in i i i in ( namely ( i > > j & 1 = 0 ) (i>>j\&1=0) (i>>j&1=0) ), So in choosing j j j The expected number of steps after that is f [ i ⊕ ( 1 < < j ) ] f[i\oplus (1<<j)] f[i⊕(1<<j)] .
So let's set up :
c n t = shape state i in has the choose Choose 了 Of Moment shape Of individual Count cnt= state i Number of rectangles that have been selected in cnt= shape state i in has the choose Choose 了 Of Moment shape Of individual Count
c = ∑ i > > j & 1 = 0 f [ i ⊕ ( 1 < < j ) ] c=\sum\limits_{i>>j\&1=0}f[i\oplus (1<<j)] c=i>>j&1=0∑f[i⊕(1<<j)]
We can get the equation ( Among them 1 1 1 In order to reach the next state, you must choose the next step , The next two fractions correspond to each other ① And circumstances ②):
f [ i ] = 1 + c n t × f [ i ] n + c n f[i]=1+\frac{cnt\times f[i]}{n}+\frac{c}{n} f[i]=1+ncnt×f[i]+nc
After finishing, there is :
f [ i ] = n + c n − c n t f[i]=\frac{n+c}{n-cnt} f[i]=n−cntn+c
Then we can find , Enumerate in reverse order i i i You can roll out all f [ i ] f[i] f[i] , Until you get the answer f [ 0 ] f[0] f[0] .
So now there is a big problem is how to know the status i i i Whether the rectangle in the can cover the whole W H WH WH , Here we divide each area into 1 1 1 The small rectangle is treated as an element , So the given n n n A rectangle can be regarded as n n n A collection of , So now we are actually asking for the union of several sets, set size , Then we can use the intersection size of its subsets to find the size of the union set , The intersection size of subsets is actually the area intersection of several rectangles , Here we maintain four boundaries to obtain . The time complexity of doing this is O ( 3 N ) O(3^N) O(3N) , Of course we can pass it S O S D P SOSDP SOSDP Optimize the time complexity to O ( 2 N N ) O(2^NN) O(2NN) .
c o d e 1 code1 code1 ( Not optimized , Directly enumerate subsets , In the code a r e a [ i ] area[i] area[i] Represents the state i i i The area of , d p [ i ] dp[i] dp[i] Represents the state i i i Area and ):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 12;
const ll mod = 998244353;
ll ksm(ll base, ll n) {
ll ans = 1;
while(n) {
if (n & 1ll) ans = ans * base % mod;
base = base * base % mod;
n >>= 1ll;
}
return ans;
}
int n;
int x[N], y[N], xx[N], yy[N], W, H;
ll dp[1 << N], area[1 << N], f[1 << N], inv[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
for (int i = 1; i <= 10; ++i) inv[i] = ksm(i, mod - 2);
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &W, &H);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d%d", &x[i], &y[i], &xx[i], &yy[i]);
}
for (int i = 1; i < (1 << n); ++i) {
int xl = 0, yl = 0, xr = W, yr = H;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
xl = max(xl, x[j]);
xr = min(xr, xx[j]);
yl = max(yl, y[j]);
yr = min(yr, yy[j]);
}
}
area[i] = 1ll * max(0, (xr - xl)) * max(0, (yr - yl));
}
for (int i = 1; i < (1 << n); ++i) {
dp[i] = 0;
for (int s = i; s; s = (s - 1) & i) {
dp[i] += area[s] * (__builtin_parity(s) ? 1 : -1);
}
}
ll SS = W * H;
if (dp[(1 << n) - 1] != SS) {
puts("-1");
continue;
}
for (int i = (1 << n) - 1; i >= 0; --i) {
f[i] = 0;
if (dp[i] == SS) continue;
ll csum = 0;
for (int j = 0; j < n; ++j) {
if (!(i >> j & 1)) {
csum = (csum + f[i ^ (1 << j)]) % mod;
}
}
f[i] = (n + csum) * inv[n - __builtin_popcount(i)] % mod;
}
printf("%lld\n", f[0]);
}
}
c o d e 2 code2 code2( S O S D P SOSDP SOSDP Optimize ):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 12;
const ll mod = 998244353;
ll ksm(ll base, ll n) {
ll ans = 1;
while(n) {
if (n & 1ll) ans = ans * base % mod;
base = base * base % mod;
n >>= 1ll;
}
return ans;
}
int n;
int x[N], y[N], xx[N], yy[N], W, H;
ll dp[1 << N], area[1 << N], f[1 << N], inv[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
for (int i = 1; i <= 10; ++i) inv[i] = ksm(i, mod - 2);
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &W, &H);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d%d", &x[i], &y[i], &xx[i], &yy[i]);
}
for (int i = 1; i < (1 << n); ++i) {
int xl = 0, yl = 0, xr = W, yr = H;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
xl = max(xl, x[j]);
xr = min(xr, xx[j]);
yl = max(yl, y[j]);
yr = min(yr, yy[j]);
}
}
area[i] = 1ll * max(0, (xr - xl)) * max(0, (yr - yl));
}
for (int j = 0; j < (1 << n); ++j) {
dp[j] = area[j] * (__builtin_parity(j) ? 1 : -1);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < (1 << n); ++j) {
if (j >> i & 1) {
dp[j] += dp[j ^ (1 << i)];
}
}
}
ll SS = W * H;
if (dp[(1 << n) - 1] != SS) {
puts("-1");
continue;
}
for (int i = (1 << n) - 1; i >= 0; --i) {
f[i] = 0;
if (dp[i] == SS) continue;
ll csum = 0;
for (int j = 0; j < n; ++j) {
if (!(i >> j & 1)) {
csum = (csum + f[i ^ (1 << j)]) % mod;
}
}
f[i] = (n + csum) * inv[n - __builtin_popcount(i)] % mod;
}
printf("%lld\n", f[0]);
}
}
版权声明
本文为[JXNU_ SONG]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230240498773.html
边栏推荐
- 全局、独享、局部路由守卫
- The usage and difference of * and & in C language and the meaning of keywords static and volatile
- Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
- Modify the content of MySQL + PHP drop-down box
- Fashion MNIST 数据集分类训练
- 下载正版Origin Pro 2022 教程 及 如何 激 活
- 高效音乐格式转换工具Music Converter Pro
- hack the box optimum靶机
- Preliminary understanding of stack and queue
- [wechat applet] set the bottom menu (tabbar) for the applet
猜你喜欢
Interpretation of the future development of smart agriculture
Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
php+mysql對下拉框搜索的內容修改
Renesas electronic MCU RT thread development and Design Competition
Fashion MNIST 数据集分类训练
全局、獨享、局部路由守衛
十六、异常检测
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
php+mysql对下拉框搜索的内容修改
Flink learning (XI) watermark
随机推荐
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
MySQL复杂查询使用临时表/with as(类似表变量)
Deploying sbert model based on torchserve < semantic similarity task >
基于Scrum进行创新和管理
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
Codeforces Round #784 (Div. 4) (A - H)题解
解决win7 中powershell挖矿占用CPU100%
本地远程访问云服务器的jupyter
Win view port occupation command line
一个国产图像分割项目重磅开源!
Efficient music format conversion tool Music Converter Pro
打靶narak
grain rain
Understanding process (multithreading primary)
Class initialization and instance initialization interview questions
Servlet template engine usage example
The interface request takes too long. Jstack observes the lock holding
Talk about current limiting
认识进程(多线程_初阶)