从大到小二进制枚举。。。。
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 5000011;
int a[50];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k, x;
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
int tmp = 0;
while (x)
{
tmp++;
x /= 2;
}
tmp--;
a[tmp]++;
}
int ans = 0;
while (n)
{
ans++;
int tmp = k;
for (int i = 30; i >= 0; i--)
{
while (tmp > 0 && a[i] > 0 && tmp >= (1 << i))
{
tmp -= (1 << i);
a[i]--;
n--;
}
}
}
printf("%d\n", ans);
}
return 0;
}
dp题,dp[i][j]表示有i个隔板,j年龄时的数量。
状态转移为dp[i][j]=dp[i-1][j]//上一个传过来时j是不变的 + dp[n-1][j-1] //因为撞到后一个所以传回来是j-1.
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 1e9 + 7;
LL dp[maxn][maxn];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i <= n; i++)dp[i][1] = 1;
for (int i = 1; i <= k; i++)dp[0][i] = 1;
for (int j = 1; j <= k; j++)
{
for (int i = 1; i <= n; i++)
{
dp[i][j] = (dp[i - 1][j] + dp[n - i][j - 1]) % mod;
}
}
printf("%lld\n", dp[n][k]);
}
return 0;
}