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

来源:互联网 发布:通达信选股软件 编辑:程序博客网 时间:2024/05/16 18:23

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进制的,现在告诉你一天有多少小时和一小时有多少分钟,问你一天里有多少个时刻,这个表上显示的数字各不相同。

思路

考虑到7进制最多只有7个数字,全排列也不过7!,可以dfs穷举然后判断这个数是否符合分钟与小时的进制。

代码

#include <cstdio>#include <algorithm>using namespace std;int a[8],ans,kn,km,n,m;int check(){    int sum=0,x=1;    for (int i=kn-1;i>=0;i--)    {        sum+=x*a[i];        x*=7;    }    if (sum>=n) return 0;    sum=0,x=1;    for (int i=km-1;i>=kn;i--)    {        sum+=x*a[i];        x*=7;    }    if (sum>=m) return 0;    return 1;}void dfs(int cnt){    if (cnt==km)    {        if (check()) ans++;    }    else    {        for (int i=0;i<7;i++)        {            int ok=1;            for (int j=0;j<cnt;j++)            {                if (i==a[j])                {                    ok=0;                    break;                }            }            if (ok)            {                a[cnt]=i;                dfs(cnt+1);            }        }    }}int main(){    int t=0,w=0;    scanf("%d %d",&n,&m);    kn=n-1;    if (!kn) t++;    while (kn)    {        kn/=7;        t++;    }    kn=t;    km=m-1;    if (!km) t++;    while (km)    {        km/=7;        t++;    }    km=t;    dfs(0);    if (km>7)    {        printf("0");        return 0;    }    printf("%d",ans);}
0 0
原创粉丝点击