Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)

来源:互联网 发布:慈溪行知职高老师 编辑:程序博客网 时间:2024/05/23 01:01
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 systemwith base7. Second, they divide one day inn hours, and each hour inm 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 from0 ton - 1, while the second has the smallest possible number of places that is necessary to display any integer from0 tom - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base7 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 aredistinct. Help her calculate this number.

Input

The first line of the input contains two integers, given in the decimal notation,n andm (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,找出(a,b)的对数,其中a满足要求:0<=a<n,a的7进制的位数和n-1的7进制的位数相同,b满足要求:0<=b<m,b的7进制的位数和m-1的7进制的位数相同,且a和b的7进制下的位上的数都不相同,即如果a的七进制数为10,b的7进制数为21,这种情况是不行的,因为重复了1


思路:枚举每一位的数,一个标记数组就可以了,当找完a再找b,注意中间的回溯,找完b检查一下就好了


ac代码:

#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headint vis[10];ll a,b;int n,m,lena,lenb;int ans;int A[10],B[10];int QAQ(int x){    int cnt=x?0:1;    while(x)        x/=7,cnt++;    return cnt;}void DFS2(int x){    if(x==lenb+1){        b=0;int k=1;        for(int j=1;j<=lenb;j++) b=b+k*B[j],k*=7;        if(b<m) ans++;        return;    }    for(int i=0;i<7;i++){        if(!vis[i]){            vis[i]=1;B[x]=i;            DFS2(x+1);            vis[i]=0;        }    }}void DFS1(int x){    if(x==lena+1){        a=0;int k=1;        for(int j=1;j<=lena;j++) a=a+k*A[j],k*=7;        if(a<n) DFS2(1);        return;    }    for(int i=0;i<7;i++){        if(!vis[i]){            vis[i]=1;A[x]=i;            DFS1(x+1);            vis[i]=0;        }    }}int main(){    scanf("%d%d",&n,&m);    lena=QAQ(n-1);    lenb=QAQ(m-1);    //cout<<lena<<' '<<lenb<<endl;    ans=0;    if(lena+lenb>7){        printf("0\n");        return 0;    }    mem(vis);DFS1(1);    printf("%d\n",ans);    return 0;}


0 0
原创粉丝点击