【HDOJ】5657 CA Loves Math
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的更多相关文章
- 【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) ...
- 【BZOJ】3561: DZY Loves Math VI
题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...
- 【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 ...
- 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]\)中各个数字互不相 ...
- 【题解】P3599 Koishi Loves Construction
[题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...
- 【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 ...
- 【BZOJ 3560】 3560: DZY Loves Math V (欧拉函数)
3560: DZY Loves Math V Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 241 Solved: 133 Description ...
- 【HDU】5269 ZYB loves Xor I
[算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...
- 【莫比乌斯反演】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, ...
随机推荐
- bzoj 2141 线段树套平衡树
用树套树来解决这个问题,存储每个节点的数值是多少,然后交换 对于答案的变更可以讨论下,假设交换的是x,y位置的数x<=y 如果x=y || high[x]=high[y]则对答案没有影响 如果h ...
- 【BZOJ】【1269】【AHOI2006】文本编辑器editor
Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...
- linux install
http://www.ubuntu.org.cn/index_kylin 先在官网下个Ubuntu 是个iso 然后下个UltraISO 是个软件 插好空u盘 打开软件 在菜单栏上 点击 文件-&g ...
- Curse of Dimensionality
Curse of Dimensionality Curse of Dimensionality refers to non-intuitive properties of data observed ...
- 【翻译】Sencha Touch2.4 The Layout System 布局
[翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...
- rdtsc获取时间统计程序的运行效率
__u64 rdtsc() { __u32 lo,hi; __asm__ __volatile__ ( "rdtsc&q ...
- flashdevelop 开发技巧
FlashDevelop用来编写AS3代码,Flash CS5用来编辑程序所需要的资源(图片,声音-),Flash CS5自带有Flex SDK,在目录 C:\Program Files\Adobe\ ...
- 线上问题 - MySQL SQL state [HY000]; error code [1366]
一.问题描述 另外一个系统调用服务接口api:/xxx/create?aName=&time=&...,数据没有保存成功提示SQL state [HY000]; error code ...
- HDU5052 Yaoge’s maximum profit(LCT)
典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换).维护的时候d ...
- 《Thinking in C++》学习笔记(二)【第三章】
第三章 C++中的C 3.4.4 指针简介 ‘&’运算符:只要在标识符前加上‘&’,就会得出标识符的地址. C和C++有一个专门存放地址的变量类型.这个变量类型叫做指针(pointer ...