Codeforces Round #359 (Div. 2) C DFS

CrossDolphin發表於2016-07-05



連結:戳這裡


C. Robbers' watch
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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~6只出現一次


思路:

第一個DFS找出左部,第二個DFS找出右部,再去判斷是否符合條件,其實看懂題目就很快會做了


程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int vis[7];
int l1=0,l2=0;
int a[10],b[10];
int ans=0;
void DFS2(int len){
    if(len==l2) {
        int now=0;
        for(int i=0;i<len;i++){
            now=now+b[i]*pow(7,(len-i-1));
        }
        if(now<m) ans++;
        return ;
    }
    for(int i=0;i<7;i++){
        if(!vis[i]){
            vis[i]=1;
            b[len]=i;
            DFS2(len+1);
            vis[i]=0;
        }
    }
}
void DFS1(int len){
    if(len==l1) {
        int now=0;
        for(int i=0;i<len;i++){
            now=now+a[i]*pow(7,(len-i-1));
        }
        if(now<n) {
         ///   for(int i=0;i<len;i++) cout<<a[i]<<" ";
         ///   cout<<endl;
            DFS2(0);
        }
        return ;
    }
    for(int i=0;i<7;i++){
        if(!vis[i]){
            vis[i]=1;
            a[len]=i;
            DFS1(len+1);
            vis[i]=0;
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    int N=n-1,M=m-1;
    while(N){
        N/=7;
        l1++;
    }
    while(M){
        M/=7;
        l2++;
    }
    if(l1==0) l1++;
    if(l2==0) l2++;
    if(l1+l2>7) {
        cout<<0<<endl;
        return 0;
    }
    DFS1(0);
    printf("%d\n",ans);
    return 0;
}


相關文章