Description
《集合论与图论》这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中。同学们不喜欢这种具有枚举性 质的题目,于是把它变成了以下问题:对于任意一个正整数 n≤100000,如何求出{1, 2,…, n} 的满足上述约束条件的子集的个数(只需输出对 1,000,000,001 取模的结果),现在这个问题就 交给你了。
Solution
考虑构造一个这样的矩阵:
x2x4x⋮3x6x12x⋮9x18x36x⋮⋯⋯⋯⋱
x
3
x
9
x
⋯
2
x
6
x
18
x
⋯
4
x
12
x
36
x
⋯
⋮
⋮
⋮
⋱
将一个满足不是 2 2 或的倍数的 x x 代入,我们取的数一定满足在矩阵中不相邻,我们将每一个都状压DP得到的结果乘起来即为答案。
Code
/************************************************
* Au: Hany01
* Date: Aug 20th, 2018
* Prob: BZOJ2734 HNOi2012 集合选数
* Email: hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int MOD = 1e9 + 1, maxn = 1e5 + 5;
int n, Ans, Sum, log3[maxn], m, t, f[2][1 << 13], lasm;
inline int ad(int x, int y) { if ((x += y) >= MOD) return x - MOD; return x; }
int main()
{
#ifdef hany01
freopen("bzoj2734.in", "r", stdin);
freopen("bzoj2734.out", "w", stdout);
#endif
n = read(), Ans = 1;
For(i, 3, n) log3[i] = log3[i / 3] + 1;
For(x, 1, n) if ((x & 1) && (x % 3)) {
t = 0, Set(f[0], 0), f[0][0] = 1, lasm = 0;
for (register int i = x; i <= n; i <<= 1) {
t ^= 1, Set(f[t], 0), m = log3[n / i] + 1;
rep(st1, 1 << lasm) if (!(st1 & (st1 >> 1)))
rep(st2, 1 << m) if (!(st2 & (st2 >> 1)) && !(st1 & st2))
f[t][st2] = ad(f[t][st2], f[t ^ 1][st1]);
lasm = m;
}
Sum = 0; rep(i, 1 << m) Sum = ad(Sum, f[t][i]);
Ans = (LL)Ans * Sum % MOD;
}
printf("%d\n", Ans);
return 0;
}