2 seconds
256 megabytes
standard input
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.
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
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+,M=1e6+,inf=1e9+;
int getnum(int x)
{
int ans=;
if(x==)
return ;
while(x)
{
x/=;
ans++;
}
return ans;
}
int a[]={,,,,,,};
pair<int,int>p;
map<pair<int,int>,int>m;
int check(int pos,int len,int x,int y)
{
int base=;
int num=;
for(int i=pos-;i>=;i--)
{
num+=a[i]*base;
base*=;
}
int shu=;
base=;
for(int i=len-;i>=pos;i--)
{
shu+=a[i]*base;
base*=;
}
if(num<=x&&shu<=y)
{
if(m[make_pair(num,shu)])
return ;
else
{
m[make_pair(num,shu)]=;
return ;
}
}
return ;
}
int main()
{
int x,y,z,i,t;
scanf("%d%d",&x,&y);
x--;
y--;
z=getnum(x)+getnum(y);
if(z>)
printf("0\n");
else
{
int ans=;
do
{
for(i=;i<z;i++)
if(check(i,z,x,y))
{
ans++;
}
}while(next_permutation(a,a+));
printf("%d\n",ans);
}
return ;
}