题目链接:http://codeforces.com/contest/686/problem/C
题目大意:
给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求a的7进制表示和b的7进制表示中的每一位都不重复。其中,a的7进制位数和n-1的7进制位数相同,b的位数和m-1的位数相同。
比如,当n=2,m=3时,a和b的其进制表示的所有集合是:
a=0, b=1
a=0, b=2
a=1, b=0
a=1, b=2
解法:
这套题目可以用dfs做。
我们开一个数组f[],f[i]表示数字i有没有在a和b的七进制表示中出现过。
然后我们遍历a和b的每一位,如果在判断这个数的第i位的时候恰好f[i]=false,我们就可以将f[i]设为true,并将这一位的值设成i,然后继续搜索。
假设我们已经通过搜索得到了a,此时边有一个确定的a(a不是我们答案里需要的,但是我们搜索的过程中实际上会遍历得到所有合法的a)以及一个确定的f[]数组。此时按照dfs_m()函数求得b(b也不是答案里需要的),如果b合法,则ans++(ans是最终答案)。
dfs_m()函数如下(用它来得到b):

void dfs_m(int tmp, int tm) {
if (tmp >= m) return;
if (tm < ) {
ans ++;
return;
}
for (int i = ; i < maxn; i ++) {
if (f[i] == false) {
f[i] = true;
dfs_m(tmp + i * g[tm], tm-);
f[i] = false;
}
}
}

我们已经有了dfs_m()了,那么只要我们在找b之前找到每一个a就行了,找a的方法也是dfs,如下的dfs_n()方法:

void dfs_n(int tmp, int tn) {
if (tmp >= n) return;
if (tn < ) {
dfs_m(, mm-);
return;
}
for (int i = ; i < maxn; i ++) {
if (f[i] == false) {
f[i] = true;
dfs_n(tmp + i * g[tn], tn-);
f[i] = false;
}
}
}

完整代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
bool f[maxn];
int g[maxn], n, m, nn, mm;
long long ans = ;
void init() {
memset(f, false, sizeof(f));
g[] = ;
for (int i = ; i < maxn; i ++)
g[i] = g[i-] * ;
}
int chk(int n) {
if (n == )
return ;
n -= ;
int cnt = ;
while (n) {
n /= ;
cnt ++;
}
return cnt;
}
void dfs_m(int tmp, int tm) {
if (tmp >= m) return;
if (tm < ) {
ans ++;
return;
}
for (int i = ; i < maxn; i ++) {
if (f[i] == false) {
f[i] = true;
dfs_m(tmp + i * g[tm], tm-);
f[i] = false;
}
}
}
void dfs_n(int tmp, int tn) {
if (tmp >= n) return;
if (tn < ) {
dfs_m(, mm-);
return;
}
for (int i = ; i < maxn; i ++) {
if (f[i] == false) {
f[i] = true;
dfs_n(tmp + i * g[tn], tn-);
f[i] = false;
}
}
} int main() {
scanf("%d%d", &n, &m);
init();
if (n > m) swap(n, m);
nn = chk(n);
mm = chk(m);
if (nn + mm > ) {
puts("");
return ;
}
dfs_n(, nn-);
cout << ans << endl;
return ;
}

Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索的更多相关文章

  1. Codeforces Round #359 (Div. 2)C - Robbers' watch

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  3. Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)

    题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...

  4. Codeforces Round #359 (Div. 2) C. Robbers' watch 鸽巢+stl

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #359 (Div. 1)

    A http://codeforces.com/contest/685/standings 题意:给你n和m,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的 ...

  6. Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs

    B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...

  7. Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题

    B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...

  8. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  9. Codeforces Round #359 (Div. 2) C

    C. Robbers' watch time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. Linux命令-目录处理命令:mv

    注意:在linux下面,剪切文件和改名是同一个命令mv,而不是两个独立的命令. mv /tmp/beijing/chaoyangqu /root 移动chaoyangqu目录到root目录下面 mv ...

  2. cocos2dx 关于lua 绑定的环境配置官方文档翻译与 将自己定义c++方法绑定到lua的的方法

    网上有好多写如何讲自己定义的方法绑定到lua的文章,当中都仅仅对环境配置做了简单的介绍,看到有的帖子写在绑定中遇到了各种各样的error.大部分是因为环境配置不对导致的,下面是官方的文档有标准的说明, ...

  3. sh 不显示当前的路径

    linux中sh是链接到bash上的,所以sh与bash在功能上是没有区别的 只是sh不显示路径!!! sh-4.1# cd /usr/local/src sh-4.1# pwd /usr/local ...

  4. Python正则表达式中的re.S的作用

    在Python的正则表达式中,有一个参数为re.S.它表示“.”(不包含外侧双引号,下同)的作用扩展到整个字符串,包括“\n”.看如下代码: import re a = '''asdfhellopas ...

  5. JqERY

    //下拉菜单样式 /*查找全部select的下拉菜单*/ function getElemsById(cot_val){ return document.getElementById(cot_val) ...

  6. CSS学习笔记(7)--html页面的CSS、DIV命名规则

    html页面的CSS.DIV命名规则 CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整 ...

  7. c#第一个程序-计算平方根

    上课教的内容.做笔记了. using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...

  8. php读取csv的问题

    csv文件要用utf-8 无bom格式保存 如果有英文外的字符,另外每项要用双引号,不用双引号不能保存非英文字符

  9. PHP——内测:新闻管理练习题及答案(自己做的)

    试题看文件:1.28练习内测:新闻管理.pdf 数据库为newssystem 表为news 表内容为 fabuxinwen.php <!DOCTYPE html PUBLIC "-// ...

  10. 使用导出导入(datapump)方式将普通表切换为分区表

    随着数据库数据量的不断增长,有些表须要由普通的堆表转换为分区表的模式. 有几种不同的方法来对此进行操作,诸如导出表数据,然后创建分区表再导入数据到分区表:使用EXCHANGE PARTITION方式来 ...