codeforces 401D. Roman and Numbers 数位dp
给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数。
用状态压缩记录哪个位置的数字已经被使用了, 具体看代码。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int digit[], m, cnt;
ll dp[<<][];
ll dfs(int s, int num) { //s是当前状态, num是模m剩余的数
if(num==&&s==)
return ;
if(~dp[s][num])
return dp[s][num];
int used[];
mem(used);
ll ret = ;
for(int i = ; i<cnt; i++) {
if(!(s&(<<i))) //如果这一位已经被使用了就直接continue
continue;
if(used[digit[i]])
continue;
used[digit[i]] = ;
ret += dfs(s^(<<i), (num*+digit[i])%m);
}
return dp[s][num] = ret;
}
int main()
{
int num[];
mem(num);
mem1(dp);
ll n;
cin>>n>>m;
while(n) {
digit[cnt++] = n%;
n/=;
}
ll ans = ;
ll s = (<<cnt)-; //初始全都没有被使用
for(int i = ; i<cnt; i++) {
if(digit[i]) {
if(num[digit[i]]) //某一位上的数不能够重复计数
continue;
num[digit[i]] = ;
ans += dfs(s^(<<i), digit[i]%m); //s^(1<<i)说明第i位被使用了
}
}
cout<<ans<<endl;
return ;
}
codeforces 401D. Roman and Numbers 数位dp的更多相关文章
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- Codeforces 401D Roman and Numbers
题目大意 Description 给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数. 注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数. ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
随机推荐
- CSS 垂直居中的5种实现方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些 ...
- SqlServer之存储过程
存储过程最主要的特色:是当写完一个存储过程后即被翻译成可执行码存储在系统表 内,当作是数据库的对象之一,一般用户只要执行存储过程,并且提供存储过程所需的参数就可以得到所要的结果而不必再去编辑 T-SQ ...
- Linux 常用命令学习
sed 大法: cat file | sed 's/string/replace_str/' file 按大小分割文件 split -b 100m filename 设置vi不自动转换tab: set ...
- Maven 打包到Tomcat下
1.在tomcat/conf下tomcat-users.xml 文件下添加 <role rolename="manager"/> <user usern ...
- std::string 字符替换函数
// 替换路径中所有“\”为“/” #include <algorithm> static std::string ConvertSlash(std::string& strUrl ...
- [php]php时间戳当中关于时区的问题
PHP_VERSION = 5.5.11 话说php函数 time() 的起始时间戳是从:GMT 1970-01-01 00:00:00 开始算起的 写了点测试代码: $gmt1 = strtotim ...
- mysql函数操作(2)
<?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...
- SQL Server 查看对象的权限
例子 1. 查看登录名 loginA的权限: create login loginA with password = '123456'; go use studio; create user logi ...
- XP下Virtualbox虚拟Ubuntu共享文件夹设置
环境:主机操作系统是Windows XP,虚拟机是Ubuntu 8.10,虚拟机是VirtualBox 2.1.0. 1. 安装增强功能包(Guest Additions) 安装好Ubuntu 8.1 ...
- javascript函数的基础功能
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...