codeforces_686C. Robbers' watch(dfs)

原创 2016年08月30日 19:03:46
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

CodeForces 685A Robbers' watch

A. Robbers' watch Robbers, who attacked the Gerda's cab, are very successful in covering ...

Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)

题目链接:点击打开链接 思路: 很简单的一道题,  dfs之后求n个数的lcm就行了, 从网上扒下来一个lcm,mdzz死循环了。。不对的代码你贴个XX 细节参见代码: #include #...

Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...

Codeforces 132C.Logo Turtle【DP,dfs】

题目大意: 在一根数轴上有一只机器龟,它能够听从人们给它的指令做出向前走一步(F)和向后转(T)的操作。给出初始操作,你最开始有修改n步指令的权利(每一个指令可以被修改很多次),问在你修改n次之后,海...

CodeForces 688C-NP-Hard Problem(dfs)

C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input sta...

Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs

题目链接:http://codeforces.com/contest/552/problem/C题意:给你100个砝码,第i个砝码质量是w^i,然后问你能不能在有m的情况下,左边和右边都放砝码,使得这...
  • yxg_123
  • yxg_123
  • 2017年02月26日 23:52
  • 78

Codeforces Round #245 (Div. 2) C. Xor-tree DFS

题目链接:http://codeforces.com/contest/430/problem/C题意:一棵以1为根节点的树,每个点都是1或者0, 选定一个节点x,当前值取反,x的孙子,孙子的孙子。。...
  • yxg_123
  • yxg_123
  • 2017年02月26日 01:16
  • 78

Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)

题目链接:点击打开链接 Description Alyona decided to go on a diet and went to the forest to get some appl...

Codeforces Round #360 (Div. 2) -- C. NP-Hard Problem (DFS二分图染色法)

C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa...

Codeforces Round #403 (Div. 2)C Andryusha and Colored Balloons (dfs)

C. Andryusha and Colored Balloonstime limit per test:2 secondsmemory limit per test:256 megabytesinp...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforces_686C. Robbers' watch(dfs)
举报原因:
原因补充:

(最多只允许输入30个字)