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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- 链接与ELF文件格式的复习
在这里复习一下链接的知识: 什么是链接(linking):把源代码形成的模块独立编译后组装成一个整体的的过程叫做链接. 链接主要过程包括:地址和空间分配(address and storage all ...
- 标签控制器,UITabBarController
注意: 1.tabbar高度不可设置,可通过_tabbar.tabbar.frame设置tabbar的位置 2.tabbar不同页面添加同一个视图后其那面添加的不起作用,只有最后一个才具有所添加的仕途 ...
- request.getSession();为什么不用response儿用request!
首先回答为什么分别是response和request这两个内置对象.你得先明白你通过获取对象是做什么用的,是往哪用的.第一个PrintWriter out=response.getWriter()是想 ...
- c++11:copy_n
copy_n: Copies exactly count values from the range beginning at first to the range beginning at resu ...
- ADO.NET笔记——使用Command执行增删改操作,通过判断ExecuteNonQuery()返回值检查是否操作成功
相关知识: ExecuteNonQuery()方法:执行CommandText属性所制定的操作,返回受影响的记录条数.该方法一般用来执行SQL中的UPDATE.INSERT和DELETE等操作 对于U ...
- PHP 图片文件上传代码
通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...
- Linux之mount命令详解
linux下挂载(mount)光盘映像文档.移动硬盘.U盘.Windows和NFS网络共享linux是个优秀的开放源码的操作系统,能够运行在大到巨型小到掌上型各类电脑系统上,随着linux系统的日渐成 ...
- gcc编译出现的问题
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error 解决办法:g++ -std=c++11
- Apache网站根目录
Apache环境配置好了,把项目放到网站根目录下的htdocs文件夹下,运行网站的时候不需要加上该文件夹的名称,Apache直接在上面找对应的项目
- matlab实现贝塞尔曲线绘图pdf查看
贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...