CF 401D 状态压缩或者hashdp

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.


好久没做cf了,突然打开一道题,看别人写法瞬间眼睛一亮,第二次认识hash的写法。

/* ***********************************************
Author :rabbit
Created Time :2014/3/16 13:59:04
File Name :3.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll dp[100010][150],num[15],base[15],val[15];
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     ll n,m;
	 while(cin>>n>>m){
		 memset(num,0,sizeof(num));
		 memset(base,0,sizeof(base));
		 memset(val,0,sizeof(val));
		 while(n){
			 num[n%10]++;
			 n/=10;
		 }
		 val[0]=1;
		 for(int i=0;i<10;i++){
			 base[i]=num[i]+1;
			 val[i+1]=base[i]*val[i];
		 }
		 dp[0][0]=1;
		 for(ll i=0;i<val[10];i++)
			 for(ll j=0;j<10;j++){
				 if(j==0&&i<val[1])continue;
				 if(i/val[j]%base[j]>=num[j])continue;
				 for(ll k=0;k<m;k++)
					 dp[i+val[j]][(k*10+j)%m]+=dp[i][k];
			 }
		 cout<<dp[val[10]-1][0]<<endl;
	 }
     return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值