TZOJ 4302 D1-Digit Divisibility DFS

Posted lhlccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于TZOJ 4302 D1-Digit Divisibility DFS的主要内容,如果未能解决你的问题,请参考以下文章

图论:TZOJ二分图练习

图论:TZOJ二分图练习

TZOJ 2462: Ferry Loading III

TZOJ 1822 Sticks DFS加剪枝

TZOJ.6865.轮渡车辆

tzoj:3613 突破包围