topcoder srm 555 div1
problem1 link
直接动态规划即可。
problem2 link
假设有$r$行,$c$列被修改了奇数次,那么一定有$r*W+c*H-2*r*c=S$。可以枚举这样的组合$(r,c)$,然后计算答案。比如对于$r$行来说,首先需要从$H$行中选出$r$行,即$C_{H}^{r}$。然后对于剩下的$Rcount-r$(一定是偶数)次修改。令$t=\frac{Rcount-r}{2}$,那么就是求有多少个$H$元组 $(a_{1},a_{2},..,a_{H})$满足$a_{i} \ge 0$且$\sum_{i=1}^{H}a_{i}=K$,运用隔板法,这个答案为 $C_{K+H-1}^{H-1}$。
problem3 link
首先,枚举初始时Head所在的位置,然后可以计算出哪些位置是可以随便放置的(因为最后会被修改)。最后,对所有的情况进行容斥。
code for problem1
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector> class CuttingBitString {
public:
int getmin(std::string S) {
const int N = static_cast<int>(S.size());
std::unordered_set<std::string> all;
long long current = 1L;
while (true) {
std::string s = GetBinary(current);
if (s.size() > S.size()) {
break;
}
all.insert(s);
current *= 5;
} auto Check = [&](int left, int right) {
return all.find(S.substr(left - 1, right - left + 1)) != all.end();
}; std::vector<int> f(N + 1, -1);
f[0] = 0;
for (int i = 1; i <= N; ++i) {
for (int j = 0; j < i; ++j) {
if (f[j] != -1 && Check(j + 1, i)) {
int t = f[j] + 1;
if (f[i] == -1 || f[i] > t) {
f[i] = t;
}
}
}
}
return f[N];
} private:
std::string GetBinary(long long x) {
std::string s = "";
while (x != 0) {
if (x % 2 == 0) {
s += '0';
} else {
s += '1';
}
x >>= 1;
}
std::reverse(s.begin(), s.end());
return s;
}
};
code for problem2
#include <algorithm>
#include <iostream>
#include <vector> const int MAX_N = 2332;
const int MAX_M = 1555; int binomial[MAX_N + 1][MAX_M + 1]; class XorBoard {
static constexpr int mod = 555555555; public:
int count(int H, int W, int Rcount, int Ccount, int S) {
Initialize(std::max(Rcount, Ccount) / 2 + std::max(H, W), std::max(H, W)); auto Compute = [&](int H, int Rcount, int h) -> long long {
int t = (Rcount - h) >> 1;
return static_cast<long long>(binomial[H][h]) *
static_cast<long long>(binomial[t + H - 1][H - 1]) % mod;
}; int result = 0;
int start_row = Rcount & 1;
int start_col = Ccount & 1;
for (int i = start_row; i <= Rcount && i <= H; i += 2) {
long long part1 = Compute(H, Rcount, i);
for (int j = start_col; j <= Ccount && j <= W; j += 2) {
if (i * W + j * H - i * j * 2 != S) {
continue;
}
long long part2 = Compute(W, Ccount, j);
result += static_cast<int>(part1 * part2 % mod);
if (result >= mod) {
result -= mod;
}
}
}
return result;
} private:
void Initialize(int max_n, int max_m) {
binomial[0][0] = 1;
for (int i = 1; i <= max_n; ++i) {
binomial[i][0] = 1;
binomial[i][1] = i;
for (int j = 2; j <= max_m; ++j) {
binomial[i][j] = binomial[i - 1][j - 1] + binomial[i - 1][j];
if (binomial[i][j] >= mod) {
binomial[i][j] -= mod;
}
}
}
}
};
code for problem3
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector> class MapGuessing {
public:
long long countPatterns(std::string goal, std::vector<std::string> code) {
std::string cmd = "";
for (auto &e : code) {
cmd += e;
}
std::unordered_set<long long> all_states;
const int N = static_cast<int>(goal.size());
for (int i = 0; i < N; ++i) {
std::vector<int> types(N, -1);
int head = i;
long long state = 0;
bool tag = true;
for (size_t j = 0; j < cmd.size(); ++j) {
if (cmd[j] == '<') {
--head;
} else if (cmd[j] == '>') {
++head;
} else if (cmd[j] == '0') {
types[head] = 0;
} else {
types[head] = 1;
}
if (head < 0 || head >= N) {
tag = false;
break;
}
bool ok = true;
for (int j = 0; j < N && ok; ++j) {
if (types[j] != -1 && types[j] + '0' != goal[j]) {
ok = false;
}
}
if (ok) {
for (int j = 0; j < N && ok; ++j) {
if (types[j] != -1) {
state |= 1LL << j;
}
}
}
}
if (tag) {
all_states.insert(state);
}
}
std::vector<long long> all;
for (auto e : all_states) {
all.push_back(e);
}
return Dfs(all, 0, (1LL << N) - 1, 0);
} private:
long long Dfs(const std::vector<long long> &all_states, size_t depth,
long long current_state, int sgn) {
if (depth == all_states.size()) {
return sgn * Count(current_state);
}
if (current_state == 0) {
return 0;
}
return Dfs(all_states, depth + 1, current_state, sgn) +
Dfs(all_states, depth + 1, current_state & all_states[depth],
sgn <= 0 ? 1 : -1);
} long long Count(long long s) {
int t = 0;
while (s != 0) {
t += s & 1;
s >>= 1;
}
return 1LL << t;
}
};
topcoder srm 555 div1的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 703 div1 -3
1.给出一个包含$n$个元素的数组$x$,构造出一个有向无环图满足从节点$i$出发可以访问到的节点数为$x_{i}$. 思路:按照$x$从小到大排序.然后从前向后处理,当前节点依次与前面已经处理的节点 ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
随机推荐
- python windows 安装pandas,numpy....
用cmd进入python的安装目录的sripts文件夹下,输入pip install pandas 等它自己下载安装完成,提示
- 删除SQL Server大容量日志的方法(转)
删除SQL Server大容量日志的方法 亲自实践的方法 1.分享数据库,如果提示被其他连接占用,不能分离,刚勾上drop connections 2.复制下所有文件,一定要备份好,以防自己操作失误 ...
- QString和char*互转
1. QString转为char * // QString转QByteArray QByteArray sr = strQ.toLocal8Bit(); int len = sr.length(); ...
- Linux 压缩解压缩命令详解
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- 【2017-03-21】HTML表单及标记
一.表单 表单在网页中主要负责数据采集功能 表单格式 <form action="服务器路径" method=get(用的比较少)/post(最常用)></for ...
- js异步计时器
js中同步和异步的区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 何时需要异步 1.在可能发生等待的情况 2.等待过程中不能像 alert 一样阻塞程 ...
- Java并发编程1--synchronized关键字用法详解
1.synchronized的作用 首先synchronized可以修饰方法或代码块,可以保证同一时刻只有一个线程可以执行这个方法或代码块,从而达到同步的效果,同时可以保证共享变量的内存可见性 2.s ...
- Linux基础命令---调整程序优先级renice
renice renice指令可以重新调整程序运行的优先级,可以通过进程id.用户id.组id来修改优先级.修改组的等级,影响组内所有用户的所有进程优先级:修改用户等级,影响该用户的所有进程优先级.除 ...
- Numpy 通用函数
frompyfunc的调用格式为frompyfunc(func, nin, nout),其中func是计算单个元素的函数,nin是此函数的输入参数的个数,nout是此函数的返回值的个数 # 注:用fr ...
- kibana添加ES索引403错误解决
kibana添加ES索引时发现kibana添加索引不生效,没有创建成功只是一闪而过 查看控制台发现报错403 解决办法: curl -XPUT -H "Content-Type: appli ...