Codeforces Round #359 (Div. 2) C. Robbers' watch 鸽巢+stl

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的分钟用七进制表示,所有数字都不相同的个数,不足的位置用0补;

思路:根据鸽巢,首先最多7个数不同,用全排列next_mutation解决,一个小坑就是7为1位数,即判断位数的时候要小心

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e3+10,M=1e6+10,inf=1e9+10;
int getnum(int x)
{
    int ans=0;
    if(x==0)
    return 1;
    while(x)
    {
        x/=7;
        ans++;
    }
    return ans;
}
int a[10]={0,1,2,3,4,5,6};
pair<int,int>p;
map<pair<int,int>,int>m;
int check(int pos,int len,int x,int y)
{
    int base=1;
    int num=0;
    for(int i=pos-1;i>=0;i--)
    {
        num+=a[i]*base;
        base*=7;
    }
    int shu=0;
    base=1;
    for(int i=len-1;i>=pos;i--)
    {
        shu+=a[i]*base;
        base*=7;
    }
    if(num<=x&&shu<=y)
    {
        if(m[make_pair(num,shu)])
        return 0;
        else
        {
            m[make_pair(num,shu)]=1;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int x,y,z,i,t;
    scanf("%d%d",&x,&y);
    x--;
    y--;
    z=getnum(x)+getnum(y);
    if(z>7)
    printf("0
");
    else
    {
        int ans=0;
        do
        {
            for(i=1;i<z;i++)
            if(check(i,z,x,y))
            {
                ans++;
            }
        }while(next_permutation(a,a+7));
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5613403.html