当前位置:网站首页>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
边栏推荐
- 1215_ Hello world used by scons
- Jz76 delete duplicate nodes in linked list
- How to prevent leakage of operation and maintenance data
- A domestic image segmentation project is heavy and open source!
- C语言 171. 最近回文数
- windows MySQL8 zip安装
- [xjtu Computer Network Security and Management] session 2 Cryptographic Technology
- Rhcsa second day operation
- [XJTU computer network security and management] Lecture 2 password technology
- 学习正则表达式选项、断言
猜你喜欢

After idea is successfully connected to H2 database, there are no sub files

leangoo脑图-共享式多人协作思维导图工具分享

Talk about current limiting

Web learning record (medium)

Flink stream processing engine system learning (II)

Store consumption SMS notification template

Flink learning (XI) watermark

接口请求时间太长,jstack观察锁持有情况

认识进程(多线程_初阶)

Day18 -- stack queue
随机推荐
Go语言web中间件的使用
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
Use of go language web Middleware
A domestic image segmentation project is heavy and open source!
How to solve the complexity of project document management?
【unity3D】直播间滚动式弹幕效果
JDBC JDBC
Decision tree principle of machine learning
Leetcode cooking
Global, exclusive and local routing guard
MySQL complex query uses temporary table / with as (similar to table variable)
Fashion MNIST 数据集分类训练
Rhcsa day 1 operation
PIP install shutil reports an error
Handwritten memory pool and principle code analysis [C language]
Parental delegation model [understanding]
The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
Classification and regression tree of machine learning
After idea is successfully connected to H2 database, there are no sub files