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.
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.
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.
2 3
4
8 2
5
(1)使用7进制
(2)一天分为n个小时(0 到n - 1),每个小时分成m分钟(0到m - 1)
(3)如果n或m比7大,那么表示比7小的数时要在前面补零
给出n和m,求各位数字都不同的时间数量。
解题思路:首先判断n和m的位数,如果n和m的位数之和大于7,说明不存在符合条件的时间,输出0;小于7则枚举每一位上的数,如果符合条件且没有出现过就让答案加一,如果不符合条件或者已经出现过就不用记录。
代码如下:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
typedef pair<int,int> P;
int b[7] = {0,1,2,3,4,5,6};
map<P,int> mapp;
int n,m;
int getbit(int x)
{
int ans = 0;
if(x == 0)
return 1;
while(x){
ans++;
x /= 7;
}
return ans;
}
bool judge(int pos,int len)
{
int base = 1;
int h = 0;
for(int i = pos - 1;i >= 0;i--){
h += b[i] * base;
base *= 7;
}
int mt = 0;
base = 1;
for(int i = len - 1;i >= pos;i--){
mt += b[i] * base;
base *= 7;
}
if(h <= n && mt <= m){
if(mapp[make_pair(h,mt)])
return 0;
else{
mapp[make_pair(h,mt)] = 1;
return 1;
}
}
return 0;
}
int main()
{
scanf("%d %d",&n,&m);
int ans = 0;
--n,--m;
int p1 = getbit(n);
int p2 = getbit(m);
int p = p1 + p2;
if(p > 7)
ans = 0;
else{
do{
for(int i = 1;i < p;i++){
if(judge(i,p))
ans++;
}
}while(next_permutation(b,b + 7));
}
printf("%d\n",ans);
return 0;
}