关闭

codeforces_686C. Robbers' watch(dfs)

171人阅读 评论(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).


特判n或m为1的情况,然后dfs。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int numN, numM;
LL ans = 0, n, m, fn, fm;
int vis[7];
LL power[30];

int solve(int a)
{
    int n = 0;
    while(a)
    {
        a /= 7;
        n++;
    }
    return n;
}

void init()
{
    memset(vis, 0, sizeof(vis));
    memset(power, 0, sizeof(power));
    if(n > 1)numN = solve(n-1);
    else numN = solve(n);
    if(m > 1)numM = solve(m-1);
    else numM = solve(m);
    int ma = max(numN, numM);
    LL t = 1;
    for(int i = 1; i <= ma; i++){
        power[i] = t;
        t *= 7;
    }
}

void dfs(int pos, LL num, int state)
{
    if(state == 1)
    {
        if(pos <= numN)
        {
            for(int i = 0; i <= 6; i++)
            {
                if(!vis[i])
                {
                    vis[i] = 1;
                    dfs(pos + 1, num + (LL)i * power[pos], 1);
                    vis[i] = 0;
                }
            }
        }
        else
        {
            if(num < n) dfs(1, 0, 2);
            return ;
        }
    }
    else if(state == 2)
    {
        if(pos <= numM)
        {
            for(int i = 0; i <= 6; i++)
            {
                if(!vis[i])
                {
                    vis[i] = 1;
                    dfs(pos + 1,  num + (LL)i * power[pos], 2);
                    vis[i] = 0;
                }
            }
        }
        else
        {
            if(num < m) ans++;
            return ;
        }
    }
}

int main()
{
    scanf("%lld%lld", &n, &m);
    init();
    dfs(1, 0, 1);
    printf("%lld\n", ans);
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26953次
    • 积分:2666
    • 等级:
    • 排名:第13593名
    • 原创:253篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条
    最新评论