题目链接: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. explain mysql性能优化

    1 使用explain语句去查看分析结果,如   explain select * from test1 where id=1; 会出现: id  selecttype  table  type po ...

  2. kernel4.1 ioctl调用

    在4.1内核中开发时遇到个奇怪的问题: 用户空间的ioctl无法调用到内核空间的unlock_ioctl 排查源码发现 546 int do_vfs_ioctl(struct file *filp, ...

  3. 支付宝钱包手势密码破解实战(root过的手机可直接绕过手势密码)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 背景 随着移动互联网的普及以及手机屏幕越做越大等特点,在移动设备上购物.消费已是 ...

  4. Ubuntu下如何检查文件的md5,sha-512码

    ubuntu自带程序md5sum,sha512sum md5sum filename sha512sum filename 即可.

  5. 二叉排序树及其C代码

    1.二叉排序树的定义   二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树, 或者是满足例如以下性质的二叉树 ...

  6. css语法和JS语法的对比

      CSS语法(不区分大小写) JavaScript语法(区分大小写) border border border-bottom borderBottom border-bottom-color bor ...

  7. 每日英语:Yahoo's Rally: Made in China

    The typical honeymoon doesn't last too long before the hard work of marriage begins. And so it norma ...

  8. vue+element-ui路由配置相关

    vue+element-ui路由配置相关 转自:http://www.cnblogs.com/MonaSong/p/6703804.html vue-router2中说明了,子路由前面可以不加'/', ...

  9. Logstash日志字段拆分grok

    参考和测试网站:http://grokdebug.herokuapp.com 例如:test-39.dev.abc-inc.com Mon Apr 24 13:53:58 CST 2017 2017- ...

  10. Git中保存用户名和密码

    每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential]     helper = store 再输入一次用户名密码后就可 ...