CodeForces 686C Robbers' watch (dfs)
2016-07-31 23:33
381 查看
题目链接:http://codeforces.com/problemset/problem/686/C
C. Robbers' watch
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.
First, as they know that kingdom police is bad at math, robbers use the positional numeral system
with base 7. Second, they divide one day in
n hours, and each hour in
m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from
0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from
0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base
7 than this watches have, the required number of zeroes is added at the beginning of notation.
Note that to display number 0 section of the watches is required to have at least one place.
Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are
distinct. Help her calculate this number.
Input
The first line of the input contains two integers, given in the decimal notation,
n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.
Output
Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
Examples
Input
Output
Input
Output
Note
In the first sample, possible pairs are: (0: 1),
(0: 2), (1: 0),
(1: 2).
In the second sample, possible pairs are: (02: 1),
(03: 1), (04: 1), (05: 1),
(06: 1).
题目大意:有个手表,由小时和分钟组成。而且小时和分钟是基于7进制的。现在给你n,m。问小时是0~n-1,分是0 ~m-1 .然后把这个转换成7进制表示在手表上,并且每个数字不能重复,并且位数要最大位数,如果位数不够就用0来代替
思路:DFS搜索
#include <bits/stdc++.h>
using namespace std;
int vis[10];
int ans,lena,lenb;
int A[10],B[10];
int n,m,a,b;
int getcou(int x)
{
int cou = x ? 0 : 1 ;
while (x)
{
x /= 7;
cou++;
}
return cou;
}
void DFS2(int x)
{
if(x == lenb + 1)
{
b = 0;
int temp = 1;
for (int j = 1 ; j <= lenb ; j++ )
{
b += temp * B[j];
temp *= 7;
}
if ( b < m)
{
ans ++;
}
return ;
}
for (int i = 0 ; i < 7 ; i++ )
{
if (!vis[i])
{
vis[i] = 1;
B[x] = i;
DFS2(x + 1);
vis[i] = 0;
}
}
}
void DFS1(int x)
{
if(x == lena + 1)
{
a = 0;
int temp=1;
for(int j = 1 ; j <= lena ; j++)
{
a += temp * A[j];
temp *= 7;
}
if( a < n)
DFS2(1);
return;
}
for(int i = 0 ; i < 7 ; i++)
{
if(!vis[i])
{
vis[i] = 1;
A[x] = i;
DFS1( x + 1);
vis[i]=0;
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
ans = 0;
lena = getcou(n - 1);
lenb = getcou(m - 1);
if ( lena + lenb > 7)
{
printf("0\n");
continue;
}
else
{
memset(vis,0,sizeof(vis));
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
DFS1(1);
printf("%d\n",ans);
}
}
return 0;
}
C. Robbers' watch
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.
First, as they know that kingdom police is bad at math, robbers use the positional numeral system
with base 7. Second, they divide one day in
n hours, and each hour in
m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from
0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from
0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base
7 than this watches have, the required number of zeroes is added at the beginning of notation.
Note that to display number 0 section of the watches is required to have at least one place.
Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are
distinct. Help her calculate this number.
Input
The first line of the input contains two integers, given in the decimal notation,
n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.
Output
Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
Examples
Input
2 3
Output
4
Input
8 2
Output
5
Note
In the first sample, possible pairs are: (0: 1),
(0: 2), (1: 0),
(1: 2).
In the second sample, possible pairs are: (02: 1),
(03: 1), (04: 1), (05: 1),
(06: 1).
题目大意:有个手表,由小时和分钟组成。而且小时和分钟是基于7进制的。现在给你n,m。问小时是0~n-1,分是0 ~m-1 .然后把这个转换成7进制表示在手表上,并且每个数字不能重复,并且位数要最大位数,如果位数不够就用0来代替
思路:DFS搜索
#include <bits/stdc++.h>
using namespace std;
int vis[10];
int ans,lena,lenb;
int A[10],B[10];
int n,m,a,b;
int getcou(int x)
{
int cou = x ? 0 : 1 ;
while (x)
{
x /= 7;
cou++;
}
return cou;
}
void DFS2(int x)
{
if(x == lenb + 1)
{
b = 0;
int temp = 1;
for (int j = 1 ; j <= lenb ; j++ )
{
b += temp * B[j];
temp *= 7;
}
if ( b < m)
{
ans ++;
}
return ;
}
for (int i = 0 ; i < 7 ; i++ )
{
if (!vis[i])
{
vis[i] = 1;
B[x] = i;
DFS2(x + 1);
vis[i] = 0;
}
}
}
void DFS1(int x)
{
if(x == lena + 1)
{
a = 0;
int temp=1;
for(int j = 1 ; j <= lena ; j++)
{
a += temp * A[j];
temp *= 7;
}
if( a < n)
DFS2(1);
return;
}
for(int i = 0 ; i < 7 ; i++)
{
if(!vis[i])
{
vis[i] = 1;
A[x] = i;
DFS1( x + 1);
vis[i]=0;
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
ans = 0;
lena = getcou(n - 1);
lenb = getcou(m - 1);
if ( lena + lenb > 7)
{
printf("0\n");
continue;
}
else
{
memset(vis,0,sizeof(vis));
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
DFS1(1);
printf("%d\n",ans);
}
}
return 0;
}
相关文章推荐
- C语言 - 结构体
- 操作系统 单处理器进程调度模拟实验(c++)
- 第一个Python程序——执行cmd命令
- POJ 2752 Seek the Name, Seek the Fame (kmp next数组运用)
- HDU 2087剪花布条 (kmp)
- HDU 1686Oulipo (kmp模板题)
- HDU 1711 Number Sequence(kmp模板)
- XML基础+Java解析XML +几种解析方式的性能比较
- LeetCode -146 LRU Cache
- 自定义复选框checkbox样式
- c++之const
- 102.You want to import schema objects of the HR user using Oracle Data Pump from the development dat
- Android多线程消息处理机制(一) Looper、Thread专题
- 机器学习(周志华) 参考答案 第四章 决策树 4.5
- LeetCode 53. Maximum Subarray
- 开发原则之约定大于配置
- 李洪强漫谈iOS开发[C语言-028]-逗号表达式
- 01-Shiro初识
- BestCoder Round #26
- HDU 3460 字典树