[数位dp] Codeforces 401D Roman and Numbers

题目链接:

http://codeforces.com/problemset/problem/401/D

D. Roman and Numbers
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m.

Number x is considered close to number n modulo m, if:

  • it can be obtained by rearranging the digits of number n,
  • it doesn't have any leading zeroes,
  • the remainder after dividing number x by m equals 0.

Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him.

Input

The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).

Output

In a single line print a single integer — the number of numbers close to number n modulo m.

Sample test(s)
input
104 2
output
3
input
223 4
output
1
input
7067678 8
output
47
Note

In the first sample the required numbers are: 104, 140, 410.

In the second sample the required number is 232.


题目意思:

给一个n(n<=10^18),m(m<=100),求把n中各位数字重排后组成的数(不能又前置零)能被m整除的个数。

解题思路:

数位dp

dp[s][m]表示当剩下的数组成的状态为s,已用的数字组成的余数为m时,能组成满足要求的种数。

最每一个剩下 的数枚举,去掉即可。注意每一位可能有相同的数,只需要一个。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

ll dp[1<<19][101]; //dp[s][m]表示还剩下状态为s的数,前面的余数为m时,满足要求的种数
//这样可以避免前面余数为m的情况的重复计算
ll n,m;
int cnt,va[20];

void init(ll cur)
{
    cnt=0;
    while(cur)
    {
        va[cnt++]=cur%10;
        cur/=10;
    }
}
ll dfs(ll cur,ll fi)
{
    if(!cur) //每一位已全部搞定
    {
        if(!fi)
            return 1;
        return 0;
    }
    if(dp[cur][fi]!=-1) //之前已经求,现在不需要求了
        return dp[cur][fi];

    int num[10];
    memset(num,0,sizeof(num));
    ll res=0;

    for(int i=0;i<cnt;i++)
    {
        if(cur&(1<<i)) //第i个数还在里面
        {
            if(num[va[i]])
                continue;
            num[va[i]]=1;
            res+=dfs(cur^(1<<i),(fi*10+va[i])%m); //把第i个数放进去
        }
    }
    return dp[cur][fi]=res; //保存
}

int main()
{
    //printf("%d\n",1<<18);
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%I64d%I64d",&n,&m))
   {
       init(n);
       memset(dp,-1,sizeof(dp));

       int num[10];
       memset(num,0,sizeof(num));
       int lim=(1<<cnt)-1;
       ll ans=0;

       for(int i=0;i<cnt;i++)
       {
           if(num[va[i]]||!va[i]) //注意首位不为零
                continue;
           num[va[i]]=1;
           ans+=dfs(lim^(1<<i),va[i]%m);
       }
       printf("%I64d\n",ans);

   }
   return 0;
}



你可能感兴趣的:([数位dp] Codeforces 401D Roman and Numbers)