Codeforces Round #359 (Div. 2) C DFS



链接:戳这里


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值