cf div2 235 D
4 seconds
512 megabytes
standard input
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.
The first line contains two integers: n (1 ≤ n < 1018) and m (1 ≤ m ≤ 100).
In a single line print a single integer — the number of numbers close to number n modulo m.
104 2
3
223 4
1
7067678 8
47
In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232.
状态DP dp[S | (1 << j) ][(k * 10 + a[j])] += dp[S][k]; ( s 里不包括 第j 个元素)
dp[S][k] 代表 取s 代表 所取的元素集合,k代表对m的取模,则这个数组代表在这一状态的数目
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <bitset> using namespace std; typedef long long ll; #define maxn (1 << 18) ll dp[maxn][],fac[];
int s[],num[];
int m,len = ;
ll n; void init() {
ll t = n;
while(t) {
s[len++] = t % ;
num[t % ]++;
t /= ;
} fac[] = ;
for(int i = ; i <= ; i++) {
fac[i] = fac[i - ] * i;
} for(int S = ; S < ( << len); S++) {
for(int j = ; j < m; j++) {
dp[S][j] = ;
}
}
} void solve() {
init(); dp[][] = ; for(int S = ; S < ( << len); ++S) {
for(int j = ; j < len; ++j) {
if(!(S & ( << j))) {
for(int k = ; k < m; ++k) {
if(S || s[j])
dp[S | ( << j)][(k * + s[j]) % m]
+= dp[S][k]; }
} } } for(int i = ; i < ; i++) {
if(num[i] > ) {
dp[( << len) - ][] /= fac[ num[i] ];
} }
printf("%I64d\n",dp[( << len) - ][]); } int main () { //freopen("sw.in","r",stdin); scanf("%I64d%d",&n,&m); solve(); return ; }
cf div2 235 D的更多相关文章
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 离线dfs CF div2 707 D
http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- cf div2 238 D
D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- CF div2 D BFS
http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- Geodatabase介绍
一.概述 (1)Geodatabase是什么? ArcGIS操作基于GIS文件格式和存储于地理数据库(Geodatabase)中的地理信息.Geodatabase是ArcGIS的本地数据结构,是用于编 ...
- Git tricks: Unstaging files
NOTE: Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unsta ...
- 简明Python中的一个小错误
最近在学Python,先看的是<Python基础教程>,后来经别人推荐,感觉网络上的<简明Python教程>也挺好的,在里面发现一个小错误. 网址如下:http://sebug ...
- WordPress使用360CDN替换google服务,解决WordPress打开速度变慢问题
由于wordpress新版本中默认使用了一些google服务及google字体,由于google在天朝中的一些你懂的原因,造成wordpress在打开时被拖慢,这时可以用360推出的公共库CDN服务替 ...
- Ubontu使用技巧
1. ctrl + alt + T => 打开命令行窗口 2. sudo su => 开启root权限 3. cd => 打开文件夹 4. cd "Program F ...
- Hadoop上路-04_HBase0.98.0入门
以下操作在Hadoop分布式集群基础上进行. 一.分布式环境搭建 下载:)验证 3)修改%HBASE%/conf/hbase-env.sh 4)修改$HBASE_HOME/conf/hbase-sit ...
- php intval()函数
格式:int intval(mixed $var [, int $base]); 1.intval()的返回值是整型,1或者0.可作用于数组或者对象(对象报错信息:Notice: Object of ...
- 为Eclipse设置背景色
1:打开Eclipse,在菜单栏找到Help—>Install new software.. 2:在打开的Work with中输入: Update Site - http://eclipse-c ...
- How to enables AX email functionality without Outlook
/***************************************************************** (C) Copyright DENTSPLY Internatio ...
- #ifdef预编译相关用法
#ifdef预编译相关用法主要有:(1)#ifdef XXX executing the corresponding xxx code #endif(2)#ifdef XXX executing th ...