zoukankan      html  css  js  c++  java
  • Codeforces 658A. Robbers' watch 模拟

    A. 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
    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).

    题目链接:http://codeforces.com/problemset/problem/685/A


    题意:一天有n个小时,每小时有m分钟。一个7进制表示的时钟一天内有几次钟表上出现的数字均不相同。
    思路:钟表上面一共有len位数。当len>7时直接不可能每位数字均不相同。只考虑len<=7的情况。用0,1,2,3,4,5,6里面的len位进行全排列。在选前x(1<=x<len)位作为小时hh,len-x位作为分钟mm。当hh<n&&mm<m时并且没有出现过,答案+1。

    代码:
    #include<bits/stdc++.h>
    using namespace std;
    int n,m,ans;
    int getnum(int x)
    {
        int gg=0;
        if(x==0)
            return 1;
        while(x)
        {
            x/=7;
            gg++;
        }
        return gg;
    }
    int sign[10];
    int num[10];
    map< int, map<int,int> >gg;
    int DFS(int x,int len)
    {
        int i,j;
        if(x==len)
        {
            /*
            for(i=1;i<=len;i++)
                cout<<num[i]<<" ";
            cout<<endl;
            */
            for(i=1; i<len; i++)
            {
                int hh=0,mm=0;
                int base=1;
                for(j=i; j>=1; j--)
                {
                    hh+=num[j]*base;
                    base*=7;
                }
                base=1;
                for(j=len; j>i; j--)
                {
                    mm+=num[j]*base;
                    base*=7;
                }
                //cout<<"hh="<<hh<<" mm="<<mm<<endl;
                if(hh<=n&&mm<=m)
                {
                    if(gg[hh][mm]==0) ans++;
                    gg[hh][mm]=1;
                }
                if(hh>n) break;
            }
        }
        for(i=0; i<7; i++)
        {
            if(sign[i]==0)
            {
                num[x+1]=i;
                sign[i]=1;
                DFS(x+1,len);
                sign[i]=0;
            }
        }
    }
    int main()
    {
        int i;
        scanf("%d%d",&n,&m);
        n--;
        m--;
        int len=getnum(n)+getnum(m);
        if(len>7) cout<<"0"<<endl;
        else
        {
            memset(sign,0,sizeof(sign));
            memset(num,0,sizeof(num));
            ans=0;
            DFS(0,len);
            cout<<ans<<endl;
        }
        return 0;
    }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    合理配置SQL Server的最大内存
    理解内存----优化SQL Server内存配置
    Systems Performance: Enterprise and the Cloud 读书笔记系列
    google perftools分析程序性能
    源代码分析-构建调用图
    Intel VTune性能分析器基础
    代微机原理与接口技术(第3版)课程配套实验包和电子课件
    【MySQL性能优化】MySQL常见SQL错误用法
    Linux 内核分析 -- Linux内核学习总结
    《Linux就该这么学》 软件库
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/5614971.html
Copyright © 2011-2022 走看看