1. 题目描述
对于给定的$a, n, mod, a \in [2,11], n \in [0, 10^9], mod \in [1, 10^9]$求出在$[1, a^n]$内的所有$a$进制下的数并且不含重复数字。

2. 基本思路
这题比赛的时候,没人做出来,但是基本思路大家都有。显然可以直接将$n$改写为$\min(n,a)$。
我比赛的代码TLE,思路是这样的:首先$mod$很小时,可以数位DP解;当$mod$很大时,可以先找到所有的排列然后,然后令$delta = fact(a)/fact(a-n)$,然后以这个作为循环间隔找到满足不重复的数字,然后再判断是否是$mod$的倍数。
hack的时候,其实可以直接以$mod$作为阈值解。题解也提到了这个思路。
这样原问题可以分两种情况:
(1) 大于阈值,枚举$mod$的倍数,然后判断是否包含重复数字;
(2) 小于等于阈值,数位DP。
然后,赛后交还是wa了几次。这里有几个特殊情况需要单独考虑:
(1) n = 0时,只能取1,直接判断是否为$mod$倍数。
(2) n = 1时,可以取[1, a],同样需要判断是否为$mod$倍数。
并且,数位DP是累加DP的。即长度为$[1,n]$的满足条件的总和。

3. 代码

 /*  */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define INF 0x3f3f3f3f
#define mset(a, val) memset(a, (val), sizeof(a)) #define LL __int64 const int bound = ;
const int maxn = ;
int dp[<<][bound];
int Bits[<<];
vector<int> St[maxn];
int Sz[maxn];
int a, n, mod; void solve();
void _solve(); inline int lowest(int x) {
return -x & x;
} inline int getBits(int x) {
int ret = ; while (x) {
++ret;
x -= lowest(x);
}
return ret;
} void init() {
int mst = << ; rep(i, , mst) {
Bits[i] = getBits(i);
St[Bits[i]].pb(i);
} rep(i, , maxn)
Sz[i] = SZ(St[i]);
} bool vis[];
inline bool judge(LL x) {
if (x == ) return false; memset(vis, false, sizeof(vis));
while (x) {
int tmp = x % a;
if (vis[tmp])
return false;
x /= a;
vis[tmp] = true;
}
return true;
} void solve() {
if (n == ) {
printf("%d\n", %mod== ? :);
return ;
}
if (n == ) {
int ans = ;
rep(i, , a+)
ans += i%mod == ? :;
printf("%d\n", ans);
return ;
} n = min(n, a);
if (mod > bound) {
_solve();
return ;
} int mst = <<a;
memset(dp, , sizeof(dp)); rep(j, , a)
++dp[<<j][j%mod]; rep(l, , n) {
rep(j, , Sz[l]) {
const int st = St[l][j];
if (st >= mst)
continue;
rep(k, , mod) {
const int& cnt = dp[st][k];
if (cnt == )
continue; rep(i, , a) {
if (st & (<<i))
continue; int nst = st | (<<i);
int nk = (k * a + i) % mod;
dp[nst][nk] += cnt;
}
}
}
} int ans = ; rep(l, , n+) {
rep(j, , Sz[l]) {
const int& st = St[l][j];
ans += dp[st][];
}
} printf("%d\n", ans);
} LL Pow(LL base, int n) {
LL ret = ; while (n) {
if (n & )
ret = ret * base;
base = base * base;
n >>= ;
} return ret;
} void _solve() {
LL tmp = mod, ubound = Pow(a, n);
int ans = ; while (tmp <= ubound) {
if (judge(tmp))
++ans;
tmp += mod;
} printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; init();
scanf("%d", &t);
while (t--) {
scanf("%d%d%d",&a,&n,&mod);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %ldms.\n", clock());
#endif return ;
}

【HDOJ】5657 CA Loves Math的更多相关文章

  1. 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化

    3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...

  2. 【BZOJ】3561: DZY Loves Math VI

    题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...

  3. 【BZOJ】3309: DZY Loves Math

    题意 \(T(T \le 10000)\)次询问,每次给出\(a, b(1 \le a, b \le 10^7)\),求 \[\sum_{i=1}^{a} \sum_{j=1}^{b} f((i, j ...

  4. 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]\)中各个数字互不相 ...

  5. 【题解】P3599 Koishi Loves Construction

    [题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...

  6. 【BZOJ 3561】 3561: DZY Loves Math VI (莫比乌斯,均摊log)

    3561: DZY Loves Math VI Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 205  Solved: 141 Description ...

  7. 【BZOJ 3560】 3560: DZY Loves Math V (欧拉函数)

    3560: DZY Loves Math V Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 241  Solved: 133 Description ...

  8. 【HDU】5269 ZYB loves Xor I

    [算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...

  9. 【莫比乌斯反演】BZOJ3309 DZY Loves Math

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

随机推荐

  1. 【BZOJ】【4034】【HAOI2015】T2

    树链剖分/dfs序 树上单点修改+子树修改+链查询 其实用dfs序做也可以…… 其实树链剖分就是一个特殊的dfs序嘛= =所以树链剖分也可以搞子树-(Orz ZYF) 至于为什么……你看在做剖分的时候 ...

  2. IIS8托管WCF服务

    WCF服务程序本身不能运行,需要通过其他的宿主程序进行托管才能调用WCF服务功能,常见的宿主程序有IIS,WAS,Windows服务,当然在学习WCF技术的时候一般使用控制台应用程序或WinForm程 ...

  3. VSFTPD全攻略(/etc/vsftpd/vsftpd.conf文件详解)

    /etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 #################匿名权限控制############### anonymous_enable=YE ...

  4. 单件模式(Singleton Pattern)(转)

    概述 Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程序在调用某一个类时,它是不会考 ...

  5. 使用WCF服务的客户端出现maxReceivedMessageSize异常

    使用WCF服务的客户端出现maxReceivedMessageSize异常解决方案 当使用WCF的客户端调取的数据过多时,会出现这个异常.一般情况下,系统默认值是65536,大约容纳100-200条左 ...

  6. Ruby Profiler 详解之 ruby-prof(I)

    项目地址: ruby-prof 在上一篇 Ruby 中的 Profiling 工具中,我们列举了几种最常用的 Profiler,不过只是简单介绍,这一次详细介绍一下 ruby-prof 的使用方法. ...

  7. 【mysql5.6】SQL基础

    我买了本深入浅出MySQL, 记录一下笔记. 一.数据定义语言(DDL) 1.创建数据库  create database name; 2.显示所有的数据库  show databases; 3.选择 ...

  8. Javascript 正则表达式_5

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. 安装Genymotion android模拟器

    Genymotion优点: 速度快性能好,资源占用低,系统要求512MB内存就能运行 支持 OpenGL 3D加速,可以流畅玩大型3D游戏 支持同时启动多个模拟器,可以实现软件或游戏多开 支持多种虚拟 ...

  10. lintcode :最长公共子串

    题目 最长公共子串 给出两个字符串,找到最长公共子串,并返回其长度. 样例 给出A=“ABCD”,B=“CBCE”,返回 2 注意 子串的字符应该连续的出现在原字符串中,这与子序列有所不同. 解题 注 ...