您的位置:首页 > 其它

【BZOJ4872】【SHOI2017】分手是祝愿

2018-03-24 11:50 393 查看
【题目链接】
点击打开链接

【思路要点】
几点显然的性质:
1、最少的操作次数为从大向小一次操作亮着的灯泡共用的次数。
2、上述的操作的灯泡集合是唯一的,并且操作顺序不影响结果。
3、操作集合外的灯泡后,一定要再操作一次,将其复原。
由此,我们首先求出最小操作次数\(cnt\),令\(f_i\)为操作集合中有\(i\)个灯泡,期望的操作次数。
则有\(\begin{equation}  
\left\{  
             \begin{array}{lr}  
             f_i=i(i≤k), &  \\  
             f_i=1+\frac{i}{n}f_{i-1}+\frac{n-i}{n}f_{i+1}(k<i≤N), &    
             \end{array}  
\right.  
\end{equation}  \)
直接用代入消元法解这个方程,复杂度是\(O(N)\)的,总复杂度\(O(N\sqrt{N})\)。
但这个做法只能得95分,因为有时方程未知数的系数可能在模\(100003\)意义下等于0。
另外一种思路就是令\(f_i\)表示操作集合大小从\(i\)变到\(i-1\)的期望步数。
则有\(f_i=\frac{i}{N}+\frac{N-i}{N}(1+f_i+f_{i+1})\),即\(f_i=\frac{(n-i)*f_{i+1}+N}{i}\)。
那么答案就是\(f_i\)的前缀和了。
最后将答案乘以\(N!\)输出。
时间复杂度\(O(N\sqrt{N})\)。
【代码】
/*Accepted Version*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int P = 100003;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int power(int x, int y) {
if (y == 0) return 1;
int tmp = power(x, y / 2);
if (y % 2 == 0) return 1ll * tmp * tmp % P;
else return 1ll * tmp * tmp % P * x % P;
}
bool a[MAXN];
int inv[MAXN], x[MAXN], delta[MAXN];
int main() {
int n, m;
read(n), read(m);
for (int i = 1; i <= n; i++)
read(a[i]);
int cnt = 0;
for (int i = n; i >= 1; i--)
if (a[i]) {
cnt++;
for (int j = 1; j * j <= i; j++)
if (i % j == 0) {
if (j * j == i) a[j] ^= true;
else a[j] ^= true, a[i / j] ^= true;
}
}
if (cnt <= m) {
int ans = cnt;
for (int i = 1; i <= n; i++)
ans = 1ll * ans * i % P;
writeln(ans);
return 0;
}
for (int i = 1; i <= n; i++)
inv[i] = power(i, P - 2);
for (int i = n; i >= m + 1; i--)
delta[i] = (1ll * (n - i) * delta[i + 1] + n) % P * inv[i] % P;
x[m] = m;
for (int i = m + 1; i <= cnt; i++)
x[i] = (x[i - 1] + delta[i]);
int ans = x[cnt];
for (int i = 1; i <= n; i++)
ans = 1ll * ans * i % P;
writeln(ans);
return 0;
}
/*95 Version - Divided by zero(Modulo P)*/
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int P = 100003;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int power(int x, int y) {
if (y == 0) return 1;
int tmp = power(x, y / 2);
if (y % 2 == 0) return 1ll * tmp * tmp % P;
else return 1ll * tmp * tmp % P * x % P;
}
bool a[MAXN];
int inv[MAXN], k[MAXN], b[MAXN], x[MAXN];
int main() {
int n, m;
read(n), read(m);
for (int i = 1; i <= n; i++)
read(a[i]);
int cnt = 0;
for (int i = n; i >= 1; i--)
if (a[i]) {
cnt++;
for (int j = 1; j * j <= i; j++)
if (i % j == 0) {
if (j * j == i) a[j] ^= true;
else a[j] ^= true, a[i / j] ^= true;
}
}
if (cnt <= m) {
int ans = cnt;
for (int i = 1; i <= n; i++)
ans = 1ll * ans * i % P;
writeln(ans);
return 0;
}
for (int i = 1; i <= n; i++)
inv[i] = power(i, P - 2);
k[m + 1] = 1ll * (n - m - 1) * inv
% P;
b[m + 1] = (1 + 1ll * (m + 1) * inv
% P * m) % P;
for (int i = m + 2; i <= n; i++) {
int tmp = (1 - 1ll * i * inv
% P * k[i - 1] % P + P) % P;
k[i] = 1ll * (n - i) * inv
% P;
b[i] = (1 + 1ll * i * inv
% P * b[i - 1]) % P;
tmp = power(tmp, P - 2);
k[i] = 1ll * k[i] * tmp % P;
b[i] = 1ll * b[i] * tmp % P;
}
x
= b
;
for (int i = n - 1; i >= cnt; i--)
x[i] = (b[i] + 1ll * k[i] * x[i + 1]) % P;
int ans = x[cnt];
for (int i = 1; i <= n; i++)
ans = 1ll * ans * i % P;
writeln(ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: