CF235D--状态压缩DP

解决数学问题:找出与给定数字最接近的数,模运算条件下
本博客详细介绍了如何通过数学逻辑找到与给定数字n,在模m条件下最接近的数字的数量。通过算法实现,解决了在特定条件下的数字匹配问题。
部署运行你感兴趣的模型镜像

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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 18
#define LL long long int
LL dp[1<<maxn][102];
LL digit[maxn];
LL base[maxn];
LL f[maxn];
int main()
{
	LL n,m;
	while(scanf("%I64d%I64d",&n,&m)==2)
	{
		memset(base,0,sizeof(base));
		memset(dp,0,sizeof(dp));
		int num = 0;
		while(n)
		{
			digit[num] = n%10;
			num++;
			base[n%10]++;
			n /= 10;
		}
		n = num;
		f[0] = 1;
		for(int i = 1;i < maxn;i++)
		{
			f[i] = f[i-1] * i;
		}
		LL repeat = 1;
		for(int i = 0;i < 10;i++)
		{
			repeat *= f[base[i]];
		}
		for(int i = 0;i < n;i++)
		{
			if(digit[i])
			{
				dp[1<<i][digit[i]%m] = 1;
			}
		}
		for(int i = 1;i < (1<<n);i++)
		{
			for(int j = 0;j < m;j++)
			{
				if(dp[i][j] == 0)	continue;
				for(int k = 0;k < n;k++)
				{
					if((i & (1<<k)) == 0)
					{
						dp[i | (1<<k)][(j*10+digit[k])%m] += dp[i][j];
					}
				}
			}
		}
		printf("%I64d\n",dp[(1<<n)-1][0]/repeat);
	}
	return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值