JXOJ 9.7 NOIP 放松模拟赛 总结
T1 数数
题意:有a个红球,b个黄球,c个蓝球,d个绿球排成一列,求任意相邻不同色的排列的数目
1 <= a , b, c, d <= 30 答案对1e9 + 7 取膜
用的类似数位dp的方法记忆化搜索,复杂度O(a4),我的方法可能常数有点大,但还是挺易懂的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
ll mol;
ll f[121][31][31][31][5];
int a, b, c, d;
ll dfs(int x, int red, int yellow, int blue, int last)
{
// printf("x = %d red = %d yellow = %d blue = %d last = %d green = %d\n", x, red, yellow, blue, last, x - 1 - red - yellow - blue);
if(x == (a + b + c + d))
{
if(red != a) if(last != 1) return 1; else return 0;
if(yellow != b) if(last != 2) return 1; else return 0;
if(blue != c) if(last != 3) return 1; else return 0;
if((x - 1 - red - yellow - blue) != d) if(last != 4) return 1; else return 0;
return 0;
}
if(f[x][red][yellow][blue][last] != -1) return f[x][red][yellow][blue][last];
ll ans = 0;
for(int i = 1; i <= 4; i++)
{
if(i == 1 && red == a)continue;
if(i == 2 && yellow == b)continue;
if(i == 3 && blue == c)continue;
if(i == 4 && (x - 1 - red - yellow - blue) == d)continue;
if(i != last)ans =(ans + dfs(x + 1, red + ((i == 1) ? 1 : 0) , yellow + ((i == 2) ? 1 : 0) , blue + ((i == 3) ? 1 : 0) , i)) % mol;
}
f[x][red][yellow][blue][last] = ans % mol;
return ans % mol;
}
int main()
{
mol = 1e9 + 7;
scanf("%d%d%d%d", &a, &b, &c, &d);
memset(f, -1, sizeof(f));
printf("%lld\n" , dfs(1, 0, 0, 0, 0));
return 0;
}
T2 数组
题意:有一个大小为n的数组a[],初始值为0,可以分m次让数组的某一位加1,求能生成多少种排列使得恰好有k个位置是奇数。
1 <= n, m <= 1e5 0 <= k <= n 答案对1e9 + 7 取膜
这道题的做法比较巧妙,我们需要将问题通过转化,变成一个简单的模型
首先我们将m-k,问题就转化为了把m-k分成n个偶数,每个偶数可以为0的问题
然后,因为偶数除以2可以为奇数,可以为偶数,我们再将(m-k)除以2,问题就转化为了将(m-k)/2分成n个正整数,每个整数可以为0的问题,用挡板法加排列组合可以解决,但由于涉及到除数取膜,需要用乘法逆元。
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
ll mol;
int n, m, k;
ll quickpow(ll a, int b)
{
ll ret = 1;
while(b != 0)
{
if(b&1) ret = ret * a % mol;
b >>= 1;
a = a * a % mol;
}
return ret % mol;
}
ll C(int x, int y)
{
ll ret = 1;
for(ll i = x; i >= x - y + 1; i--)
{
ret = ret * i % mol;
}
for(ll i = 1; i <= y; i++)
{
ret = ret * quickpow(i, mol - 2) % mol;
}
return ret;
}
int main()
{
mol = 1e9 + 7;
scanf("%d%d%d", &n, &m, &k);
if((m - k) % 2 == 1)
{
printf("0\n");
return 0;
}
printf("%lld\n", C(n , k) * C((m - k) / 2 + n - 1, n - 1) % mol);
return 0;
}