codeforces 686C C. Robbers' watch(dfs)

本文探讨了一种基于7进制计时系统的问题,该系统用于计算一天内所有显示时间中,小时和分钟数字各不相同的时刻总数。通过深度优先搜索算法解决此问题,并给出了完整的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

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

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5613131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值