T1:
f[i]:用状态为i的数字构成小于等于n的数字的方案数
g[i]:用状态为i的数字构成的合法好集的方案数
f[i]:就算N = 1E9,总的合法的数字也就500W,枚举一发
g[o] = ∑f[op]*g[o - op]
强制op含有o最左边的1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn = 1<<10;
typedef long long LL;
const LL mo = 1000000007LL;
struct data{
int num,pos;
data(int _num = 0,int _pos = 0) {num = _num; pos = _pos;}
bool operator < (const data &b) const {
return num < b.num;
}
}Q[20];
int n,k,tot,now[11],bo[11],ans[20];
LL f[maxn][20],g[maxn][11],fac[11];
void cal(int x,int o,int sum)
{
if (1LL*sum*fac[tot-x+1] > Q[n].num) return;
if (x > tot) {
for (int i = 1; i <= n; i++)
if (Q[i].num >= sum) {
++f[o][i]; break;
}
return;
}
int Start = (x == 1 && now[1] == 0)?2:1;
for (; Start <= tot; Start++)
if (!bo[Start]) {
bo[Start] = 1;
cal(x+1,o,sum*10 + now[Start]);
bo[Start] = 0;
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("subsets.in","r",stdin);
freopen("subsets.out","w",stdout);
#endif
fac[0] = 1;
for (int i = 1; i <= 10; i++) fac[i] = fac[i-1]*10LL;
for (;;) {
int x; scanf("%d",&x);
if (!x) break;
Q[++n] = data(x,n);
}
sort(Q + 1,Q + n + 1);
for (int o = 2; o < maxn; o++) {
tot = 0; int po = 0;
for (int x = o; x; x >>= 1) {
if (x&1) now[++tot] = po;
++po;
}
cal(1,o,0);
}
for (int i = 1; i <= n; i++) {
memset(g,0,sizeof(g));
LL Ans = 0; g[0][0] = 1;
for (int o = 2; o < maxn; o++) {
f[o][i] += f[o][i-1];
int po = 0; int x = 0;
for (int y = o; y; y >>= 1) {
if (y&1) po = x;
++x;
}
int opt = o - (1<<po);
g[o][1] = f[o][i]; Ans = (Ans + f[o][i]) % mo;
for (int j = 2; j <= 10; j++) {
for (int op = (opt - 1) & opt; op; op = (op - 1) & opt)
g[o][j] = (g[o][j] + f[(1<<po) + op][i]*g[opt-op][j-1])%mo;
g[o][j] = (g[o][j] + f[1<<po][i]*g[opt][j-1])%mo;
Ans = (Ans + g[o][j]) % mo;
}
}
ans[Q[i].pos] = Ans;
}
for (int i = 1; i <= n; i++) printf("%d\n",ans[i]);
return 0;
}
MLGB。。现场没写freopen
预处理询问的大小顺序,可以只进行一次dfs,提高效率
T2:贪心待填
T3:
枚举一个子序列,将数字插入值域树状数组,二分当前的答案
O(n^2log^2n)强行过
话说,树状数组常数小
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn = 6E3 + 10;
const int maxm = 1E6 + 10;
const int T = 1000001;
int n,a[maxn],ans[maxm],c[maxm],tot[maxm];
void Inc(int pos)
{
for (int j = pos; j <= T; j += j&-j) ++c[j];
}
void Dec(int pos)
{
for (int j = pos; j <= T; j += j&-j) --c[j];
}
int Sum(int pos)
{
int ret = 0;
for (int j = pos; j >= 1; j -= j&-j) ret += c[j];
return ret;
}
int Search(int l,int r,int goal)
{
if (l == r) return l;
int mid = (l + r) >> 1;
int sl = Sum(mid - 1);
if (sl + tot[mid] < goal) return Search(mid+1,r,goal);
else return Search(l,mid,goal);
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("mid.in","r",stdin);
freopen("mid.out","w",stdout);
#endif
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d",&a[i]),++a[i];
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
++tot[a[j]]; Inc(a[j]);
int len = j - i + 1;
int goal = len/2 + 1;
int pos = Search(1,T,goal);
++ans[pos];
}
for (int j = i; j <= n; j++) {--tot[a[j]]; Dec(a[j]);}
}
for (int i = 1; i <= T; i++)
if (ans[i])
printf("%d %d\n",i - 1,ans[i]);
return 0;
}