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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- Struts2+Hibernate4+Spring4整合
jar包 配置文件 web.xml文件 <!-- needed for ContextLoaderListener --> <context-param> <param- ...
- java synchronized关键字浅探
synchronized 是 java 多线程编程中用于使线程之间的操作串行化的关键字.这种措施类似于数据库中使用排他锁实现并发控制,但是有所不同的是,数据库中是对数据对象加锁,而 java 则是对将 ...
- C++ 11 之初始化
1.4中不同初始化的形式 a.string s("zhl").int i(3); //括号初始化 b.string s="zhl".int ...
- java 设计模式之单例模式
-------Success is getting what you want, happiness is wanting what you get. java设计模式之单例模式(Singleton) ...
- FPGA笔记-读取.dat文件
读取.dat图像文件 .dat文件是matlab生成的图像文件 initial begin // Initialize Inputs CLK = 0; RST = 1; IMAGE_DATA = 0; ...
- WP开发笔记——WP7 SDK使用技巧
俗话说的好,工欲善其事,必先利其器. 入门WP开发之前,免不了要先接触开发环境和开发工具.使用WP7 SDK进行开发,我们需要掌握SDK的一些实用技巧,以便我们的开发. 一.开启/关闭电脑键盘输入 W ...
- 省市区联动(MVC分布视图)
1.调用分布视图 //Html辅助方法 返回参数的值 存储在ProvinceId.CityId.DistrictId中 @{Html.RenderAction("GetProvince&qu ...
- SequoiaDB 1.5 版本发布
SequoiaDB 1.5 – 2013.11.13 新特性 1. 新增聚合特性,API实现 GROUPBY, MAX 等功能: 2. 全新改版的Web管理界面: 3. 提供C#语言 ...
- openerp 经典收藏 Openerp开发进销存系统完毕总结(转载)
原文地址:http://blog.csdn.net/heartrude/article/details/9142463 Openerp开发进销存系统完毕总结 分类: 代码历程 OpenERP 工程思想 ...
- Is C# a clone of a Microsoft replacement for Java?
Is C# a clone of a Microsoft replacement for Java?Let's look at what Anders Hejlsberg Said. Hejlsber ...