HDU 5657 CA Loves Math 状压DP + 枚举
题意:
给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\)。
求区间\([1, A^n]\)中各个数字互不相同的\(A\)进制数而且是\(k\)的倍数的个数。
分析:
如果\(n > A\),根据抽屉原理,\(n\)位\(A\)进制数一定会有重复的数字。
所以下面只讨论\(n \leq a\)的情况。
对于\(k\)的大小,分别使用不同的算法:
\(k\)比较小的时候,用状压DP:\(d(S, x)\)表示当前数字集合为\(S\)且模\(k\)为\(x\)的数字的个数。
在数字的末位添加一个未在集合\(S\)出现的数字\(i\),则转移到状态\(d(S',(x \cdot A + i) % k)\)。\(k\)比较大的时候,直接暴力统计。枚举区间内所有\(k\)的倍数,判断一下如果没有重复数字答案+1。
第一种算法的复杂度为\(O(2^A \cdot k \cdot A)\),状态数乘转移数。
第二种算法复杂度为\(O(A^n/k \cdot A)\),枚举次数乘判断时间。
找到一个合适的边界使得时限都能承受两种算法,测试了一下\(25000\)和\(30000\),运行时间都是\(3s\)多,可以AC。
PS:\(n=0\)和\(n=1\)这些边界情况最好特判一下。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int A, n, k;
const int maxk = 30000;
const int maxa = 11;
LL d[1 << maxa][maxk];
int bitcount(int x) {
int ans = 0;
while(x) { if(x & 1) ans++; x >>= 1; }
return ans;
}
bool check(LL x) {
int S = 0;
while(x) {
int t = x % A;
x /= A;
if(S & (1 << t)) return false;
S |= (1 << t);
}
return true;
}
int main()
{
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &A, &n, &k);
if(n == 0) {
if(k == 1) printf("1\n");
else printf("0\n");
continue;
}
if(n == 1) {
printf("%d\n", A / k);
continue;
}
//DP
if(k < maxk) {
memset(d, 0, sizeof(d));
for(int i = 1; i < A; i++) d[1 << i][i % k] = 1;
for(int S = 0; S < (1 << A); S++) if(bitcount(S) < n) {
for(int x = 0; x < k; x++) if(d[S][x]) {
for(int i = 0; i < A; i++) if(!(S&(1<<i))) {
d[S|(1<<i)][(x*A+i)%k] += d[S][x];
}
}
}
LL ans = 0;
for(int S = 0; S < (1 << A); S++)
if(d[S][0]) ans += d[S][0];
printf("%lld\n", ans);
} else {
if(n > A) n = A;
LL An = 1; //A^n
for(int i = 1; i <= n; i++) An *= A;
LL ans = 0;
for(LL t = k; t <= An; t += k)
if(check(t)) ans++;
printf("%lld\n", ans);
}
}
return 0;
}
HDU 5657 CA Loves Math 状压DP + 枚举的更多相关文章
- [Luogu P3959] 宝藏 (状压DP+枚举子集)
题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...
- HDU 6149 Valley Numer II 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意:中文题目 解法:状压DP,dp[i][j]代表前i个低点,当前高点状态为j的方案数,然后枚 ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU - 4284 Travel(floyd+状压dp)
Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...
- HDU 4906 Our happy ending (状压DP)
HDU 4906 Our happy ending pid=4906" style="">题目链接 题意:给定n个数字,每一个数字能够是0-l,要选当中一些数字.然 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- HDU 4568 Hunter 最短路+状压DP
题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...
随机推荐
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- linux系统的安全小知识
最近安装linux虚拟机,出了几个小问题:1. 只有root用户 2.ftp连接不上 3.ssh连接虚拟机如何免密 1.创建用户 useradd –d /usr/sam -m sam 创建用 ...
- nodejs的会话总结
前言: http是一个无状态协议,所以客户端每次发出请求时,下一次请求就无法得知上一次请求所包含的状态数据,那么如何能把一个用户的状态数据关联起来?1.cookie 一开始,人们采用cookie这门技 ...
- Visual Studio 2010 vs2010 英文版 使用 已有的中文版 MSDN 帮助文档
第一步 设置Help Library Manager区域语言 打开Microsoft Visual Studio 2010开始菜单里Visual Studio Tools里的Manage Help S ...
- No module named 'revoscalepy'问题解决
SqlServer2017开始支持Python,前段时间体验了下,按照微软的入门例子操作的:https://microsoft.github.io/sql-ml-tutorials/python/re ...
- Yii2 Working with Relational Data at ActiveDataProvider
Yii2 Working with Relational Data at ActiveDataProvider namespace common\models; use Yii; use yii\ba ...
- java +selenuim使用js显示控件
操作selenium控件时,往往需要有些控件提前显示,特别是后台的一些控件,或者需要使用鼠标显示下拉的操作,有时鼠标悬停,在点击不怎么好使,就可以依靠js方式,提前让控件显示. 1.使用style的方 ...
- World Wind Java开发之四——搭建本地WMS服务器(转)
在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...
- Linux学习记录(三)
1.Linux的软件安装 1.1.jdk安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | g ...
- tomcat服务器用Servlet类查找磁盘文件上的Json信息,如果匹配则在浏览器上显示出该条内容的全部信息
package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...