CodeForces - 686C Robbers' watch

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).

题意:要求小时h和分钟m都是以7进制显示,小时和分钟每一位数字都必须不同,问符合条件的时间组合数。

思路:大数据只是纸老虎! 因为是7进制,所以出现的数只有0~6七个。又因为每个数只能出现一次,所以小时和分钟的位数最多7位。所以先判断位数,然后枚举。

#include<stdio.h>
#include<string.h>
int vis[7];
int judge(int x,int l)
{
    int sum=0;
    while(x>0)
    {
        int t=x%7;
        if(vis[t]==1)
            return true;
        vis[t]=1;
        x=x/7;
        sum++;
    }
    while(sum<l)
    {
        if(vis[0]==0)
            vis[0]=1;
        else
            return true;
        sum++;
    }
    return false;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int len1=1;
        int len2=1;
        int i,j;
        for(i=7;i<n;i*=7)
            len1++;
        for(i=7;i<m;i*=7)
            len2++;
        int ans=0;
        if(len1+len2<=7)
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    memset(vis,0,sizeof(vis));
                    if(i==j||judge(i,len1)||judge(j,len2))
                        continue;
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值