您的位置:首页 > 其它

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
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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息