codeforces 686C C. Robbers' watch(dfs)

题目链接:

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

题意:

每天n小时,每小时m分钟,现在给你一个7进制的表,问出现的所有的时间中数字全不相同的时间有多少个;

思路:

先找出表上有多少位数字,再按位dfs,看最后得到的数是否<n和<m,计数就好;

AC代码:

//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=(1<<18)+10;
const int maxn=1005;


LL n,m,ans=0;
int a[20],b[20],cnt1=0,cnt2=0,vis[10];
LL fn,fm;
LL p[25];


void DFS2(int po,LL num)
{
    if(po>cnt2)
    {
        if(num<fm)ans++;
        return ;
    }
    for(int i=0;i<7;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            DFS2(po+1,num+(LL)i*p[po]);
            vis[i]=0;
        }
    }

}

void DFS1(int po,LL num)
{
    if(po>cnt1)
    {
        if(num<fn){DFS2(1,0);}
        return ;
    }
    for(int i=0;i<7;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            DFS1(po+1,num+(LL)i*p[po]);
            vis[i]=0;
        }
    }
}

int main()
{
            read(n);read(m);
            fn=n,fm=m;
            if(n>1)n--;
            if(m>1)m--;
            LL w=1;
            for(int i=1;i<25;i++)
            {
                p[i]=w;
                w=w*7;
            }
            while(n)
            {
                a[++cnt1]=n%7;
                n/=7;
            }
            while(m)
            {
                b[++cnt2]=m%7;
                m/=7;
            }
            mst(vis,0);
            ans=0;
            DFS1(1,0);
            cout<<ans<<"\n";
        return 0;
}

 

posted @ 2016-06-24 09:39  LittlePointer  阅读(461)  评论(0编辑  收藏  举报