题目链接

给出一个<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的更多相关文章

  1. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  2. Codeforces 401D Roman and Numbers

    题目大意 Description 给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数. 注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数. ...

  3. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  4. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. 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 ...

  6. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  7. Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)

    题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...

  8. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  9. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

随机推荐

  1. React系列(一):React入门

    React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...

  2. Android Studio的配置

    第一次使用Android Studio时你应该知道的一切配置 - 生命壹号 - 博客园http://www.cnblogs.com/smyhvae/p/4390905.html

  3. hadoop 各种组件配置参数

    ********************************************hive*********************************************** hive ...

  4. LINUX下查看CPU使用率的命令[Z]

    1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...

  5. iOS堆栈-内存-代码在据算机中的运行

    其实作程序不管是那行,学什么语言最终的目的是和就算机打交道的,我们写的程序计算机是怎么处理的呢??? 计算机运行我们的程序无非就是吧磁盘-内存-cpu三者结合起来 我们写一个程序代码肯定是在此盘中存着 ...

  6. Python框架

     Django.Pylons & TurboGears & repoze.bfg.Tornado & web.py.Bottle & Flask.Quixote(豆瓣用 ...

  7. subline text 3的模版设置

    文件目录在这个地方,然后用St3打开,进行模版修改就行了

  8. 非阻塞IO

    设置描述符非阻塞的两种方法: 1,调用 open 时,设置,O_NONBLOCK; 2,调用 fcntl设置: 具体如下: ,open("/xxx/file1",O_RDWR|O_ ...

  9. css 清除浮动 兼容IE+, FF

    上代码: <!doctype html> <html> <head> <meta charset="utf-8"> <titl ...

  10. python request的运用

    requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: python的标准库urllib ...