• TZOJ 4302 D1-Digit Divisibility DFS


    Using each of the digits 1, 2, 3,...,D1 exactly once to form D1 digit numbers, how many are divisible by D2.
     

    输入

     

    The input data will contain multiple cases. Each case will contain two whole numbers, D1 and D2. D1 will represent the number of unique digits (starting at 1), that will form the number. 3 <= D1 <= 9. D2 represents the divisor. 2 <= D2 <= 50.
     

    输出

     

    Each case will output the number of different D1-digit numbers that are divisible by D2 in one line.
     

    样例输入

    5  12

    4   6

    样例输出

     24

     0

    题意  用1到n组成不重复的n位数,输出能被m整除的个数。

    要点 DFS求全排列

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 200005
    int n,vis[10],num[10],mm;
    int js;
    int DFS(int m)
    {
        if (m==n)
        {
            int sum=0;
            for (int i=0;i<m;i++)
              {
                  sum=sum*10+num[i];
              }
              if(sum%mm==0)
                  js++;
            return sum;
        }
        for (int i=1;i<=n;i++)//理解记住
        {
            if (!vis[i])
            {
            vis[i]=1;
            num[m]=i;
            DFS(m+1);
            vis[i]=0;
            }
        }
    }
    int main()
    {
        while(scanf("%d %d",&n,&mm)!=EOF)
        {
            js=0;
            memset(vis,0,sizeof(vis));
            DFS(0);
            cout<<js<<endl;
        }
    
        return 0;
    }
  • 相关阅读:
    图片放大功能
    谈论算法
    socket基础
    js实现快速排序
    mysql死锁问题分析(转)
    MVCC 专题
    ActiveMQ持久化方式(转)
    消息队列中点对点与发布订阅区别(good)
    tomcat下部署activemq(转)
    Android文件下载(实现断点续传)
  • 原文地址:https://www.cnblogs.com/lhlccc/p/11567193.html
Copyright © 2020-2023  润新知