比赛链接

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;
}

T3 子集

题意:有一个集合,里面有n个正整数。这个集合有2n个子集,求这个集合的第k小子集

1 <= n <= 35

首先,我们通过观察发现,对于40%的数据,我们直接暴力搜索就可以解决问题,但总范围刚好是我们搜索范围的两倍,对于这一类问题,我们可以采用一种叫做meet-in-the-middle的算法,类似于广搜优化技巧中的双向广搜。

我们每次只搜索总范围的一半,这样时间和空间就不会出现问题,然后对每次搜索出来的数进行排序,使数据有序,然后利用数据的有序,采用二分,用两个指针分别在两个数组中查找即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
int n;
ll k;
int r1, r2;
ll a[3][1 << 20], b[50], c1[1 << 20], c2[1 << 20];
int head[3], tail[3];
ll cnt[3];
void dfs(int x, ll num, int k)
{
// printf("x = %d num = %lld k = %d\n", x, num, k);
if(x == tail[k] - head[k] + 1)
{
a[k][++cnt[k]] = num;
return;
}
dfs(x + 1, num + b[x + head[k] - 1 + 1], k);
dfs(x + 1, num, k);
}
int check(ll x)
{
ll p1 = 1;
while(c1[p1 + 1] + c2[1] <= x && p1 + 1 <= r1)p1++;
ll cnt = 0;
for(int i = 1; i <= r2; i++)
{
while(c1[p1] + c2[i] > x && p1 >= 0) p1--;
if(p1 < 0)break;
cnt += (p1);
}
// printf("cnt = %lld\n", cnt);
if(cnt >= k)return 1; else return 0;
}
int main()
{
scanf("%d%lld", &n, &k);
for(int i = 1; i <= n; i++)scanf("%lld", &b[i]);
head[1] = 1; tail[1] = n / 2; r1 = tail[1];
head[2] = n / 2 + 1; tail[2] = n; r2 = tail[2] - head[2] + 1;
dfs(0, 0, 1);dfs(0, 0, 2);
r1 = cnt[1]; r2 = cnt[2];
for(int i = 1; i <= r1; i++)c1[i] = a[1][i];
for(int i = 1; i <= r2; i++)c2[i] = a[2][i];
sort(c1 + 1, c1 + r1 + 1);
sort(c2 + 1, c2 + r2 + 1);
// for(int i = 1; i <= r1; i++) printf("%d ", c1[i]); printf("\n");
// for(int i = 1; i <= r2; i++) printf("%d ", c2[i]); printf("\n");
// printf("r1 = %d r2 = %d\n", r1, r2);
ll l = 1, r = 35, ans = 0; r *= 1e9;
while(l <= r)
{
// printf("l = %lld r = %lld\n", l, r);
ll mid = (l + r) >> 1;
if(check(mid) == 1)
{
ans = mid; r = mid - 1;
}
else l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}

JXOJ 9.7 NOIP 放松模拟赛 总结的更多相关文章

  1. NOIP前模拟赛总结

    NOIP前模拟赛总结 from 2018.10.7 to ??? Date Name Score(Rank) Problems 2018.10.7 McfXH AK Contest 42(?) 期望得 ...

  2. NOIP一系列模拟赛小结

    NOIP越发接近了,于是自己也跟着机房的几位师兄一起做了几次NOIP模拟赛,收获颇多. #1-T1:求点集中的点能否只用三条与坐标轴平行的直线就能全部被经过,其实只要将横纵坐标排序后逐个点检查下就行. ...

  3. NOIP欢乐模拟赛 T1 解题报告

    小澳的方阵 (matrix.cpp/c/pas) [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑 ...

  4. LUOGU NOIP 2018 模拟赛 DAY1

    T1 传送门 解题思路 这似乎是小学数学知识???mod 9就相当于各位之和mod 9,打表求了个逆元,等差数列求和公式就行了. #include<iostream> #include&l ...

  5. 【NOIP考前模拟赛】纯数学方法推导——旅行者问题

    一.写在前面 这题似乎是一道原创题目(不是博主原创),所以并不能在任何OJ上评测,博主在网盘上上传了数据(网盘地址:http://pan.baidu.com/s/1mibdMXi),诸位看官需者自取. ...

  6. NOIP欢乐模拟赛 T3 解题报告

    3.小澳的葫芦 (calabash.cpp/c/pas) [题目描述] 小澳最喜欢的歌曲就是<葫芦娃>. 一日表演唱歌,他尽了洪荒之力,唱响心中圣歌. 随之,小澳进入了葫芦世界. 葫芦世界 ...

  7. NOIP欢乐模拟赛 T2 解题报告

    小澳的坐标系 (coordinate.cpp/c/pas) [题目描述] 小澳者表也,数学者景也,表动则景随矣. 小澳不喜欢数学,可数学却待小澳如初恋,小澳睡觉的时候也不放过. 小澳的梦境中出现了一个 ...

  8. 6.19noip模拟赛总结

    昨天进行了noip的模拟赛,我这个蒟蒻又是垫底.... T1 第一感觉就是贪心,从高到低排序,然后每次都将恰好满足当前条件的人数分成一组,然后移动到下一个未分组的单位上,贴代码 #include< ...

  9. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

随机推荐

  1. 导出excel的功能效果实现

    <el-button @click="exportExcel" > <i style="display: inline-block;"> ...

  2. Linux学习之如何让普通用户获得ROOT权限

    https://blog.csdn.net/qq_41940950/article/details/81044594

  3. We Need More Bosses CodeForces - 1000E (无向图缩点)

    大意: 给定无向连通图, 定义两个点$s,t$个价值为切断一条边可以使$s,t$不连通的边数. 求最大价值. 显然只有桥会产生贡献. 先对边双连通分量缩点建树, 然后求直径即为答案. #include ...

  4. (三)Lucene之删除更新文档以及luke的基本使用

    一.demo 本例中采用单元测试,故在pom.xml中引入junit jar包 1.1 前提: public class IndexTest { /** *数据准备 */ private String ...

  5. SQL Server系统函数:字符串函数

    原文:SQL Server系统函数:字符串函数 1.字符转化为ASCII,把ASCII转化为字符,注意返回的值是十进制数 select ASCII('A'),ASCII('B'),ASCII('a') ...

  6. Lua 可变参数 ... 的一点测试

    function test( ... ) if (...) then dibug("has ...") else dibug("no ...") end for ...

  7. 如何在 vue 2.0+ 中引入全局的stylus文件,且能正常

    由于stylus在引用时,不能像一般的css文件直接在main.js中引用,就算引用了也会只能使用转换后的css,不能使用里面的函数,方法等,原因可能是:在这里引入会被直接编译成css,不能在别的模板 ...

  8. ES6箭头函数及this指向

    箭头函数(=>):函数简写 无参数:() => {} 单个参数:x => {} 多个参数:(x, y) => {} 解构参数:({x, y}) => {} 嵌套使用:部署 ...

  9. 运行 jar 的问题

    lib stwe.jar 同目录

  10. python多线程与多进程异步事件框架

    多线程简单实现 #!/usr/bin/env python # -*- coding: UTF-8 -*- import logging import queue import threading f ...