题目链接: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. Eclipse+PyDev 安装和配置(转)

    Python开发有很多工具,其中Eclipse+Pydev 是最常见的一种.本文简单介绍Windows下Eclipse+PyDev 安装和配置. Eclipse 是一种基于 Java 的可扩展开源开发 ...

  2. 摘:strings(字符串)简介

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是 ...

  3. 四种对象生存期和作用域、static 用法总结

    一.四种对象生存期和作用域 栈对象 隐含调用构造函数(程序中没有显式调用) 堆对象 隐含调用构造函数(程序中没有显式调用),要显式释放 全局对象.静态全局对象 全局对象的构造先于main函数 已初始化 ...

  4. opensips编译安装时可能遇到的问题

    错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...

  5. 使用TestFlight邀请外部人员測试APP

    怎样使用TestFlight邀请外部人员測试APP 详细过程例如以下: 1.在邀请測试人员的时候.按上线流程打包APP,提交. 2.提交审核,在邀请測试员的时候,你必须先提交审核,苹果会在大约2个工作 ...

  6. Android Studio Share Project On Github

    在Android Studio上将项目导入Github上: 1. 创建一个Project.点击VCS -> Enable Version Control Integration... 2.选择G ...

  7. centos 无法ping内网 Destination Host Unreachable

    centos 突然无法ping内网了. 本来是一直是好好的. 在这之前,当前服务器(centos 192.168.1.30)大量的在操作内网192.168.1.20服务器的数据库.. 会不会是流量大了 ...

  8. (void)0和0的区别及用法

    (void)0相当于宏NULL,NULL本身的含义为“空”,在c语言代表“不存在.不确定”的含义. 0不能简单的理解为“没有”的意思,在c语言及二进制中,0和1代表的是“一件事物的正反两个方面“,0是 ...

  9. Xilinx问题查找

    1.登录https://www.xilinx.com/ 2.在All下拉菜单选择Support选项查找技术问题,All选项会查找全部关键词,但是大多数我们只需要技术相关的内容

  10. Linux Shell编程之常用技巧

    前言 本文集中介绍了bash编程中部分高级编程方法和技巧.通过学习本文内容,可以帮你解决以下问题: 1.bash可以网络编程么? 2..(){ .|.& };. 据说执行这些符号可以死机,那么 ...