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- ...
随机推荐
- sql server 存储过程分隔split
CREATE FUNCTION [dbo].[F_split] ( ), ) ) , ), f )) --实现split功能 的函数 AS BEGIN DECLARE @i INT SET @Sour ...
- asp.net预览图片
Aspx code <table> <tr> <td class="style3"> <asp:Label ID="Label1 ...
- 第10课_dg
export ORACLE_BASE=/u01/app/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORACLE ...
- 让Linux修改IP、DNS等可以更简单
修改IP: 可以用 netconfig,可惜每次都得输入完整的IP.掩码.网关和DNS. 不如直接 vi /etc/sysconfig/network-scripts/ifcfg-eth0 再 /et ...
- APP-SQLAP-10771
用户在匹配 PO时,其中一张AP INVOICE不管进行何种更新操作,总是报:APP-SQLAP-10771:Could not reserve record(不能保留记录) 错误. 而且这个发票问题 ...
- 【转】java jawin api 中文 invoke方法
org.jawin Class FuncPtr java.lang.Object org.jawin.FuncPtr ----------------------------------------- ...
- http keepalive and tcpkeepalive
http keepalive 减少tcp 连接 (三次握手的消耗) tcp keepalive 检测死链接的 session 在tcp连接中, src_ip + src_port + dest_ip ...
- PHP设计模式之单例模式(数据库访问)
1.什么是单例模式? 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 2.单例模式的 ...
- Viewing the Raw SQL Statement(xcode で)
Thanks to Core Data. Even without learning SQL and database, you’re able to perform create, select, ...
- windows下python2和python3共存
相信很多朋友都在网上搜索过python多版本共存的问题. 多说的说法都是修改python.exe的名字为python2.exe或者python3.exe. 但是我按照这样的方法却总是不成功. 修改py ...