nowcoder A hard problem /// 数位DP
题目大意:
称一个数x的各个数位之和为f(x)
求区间L R之间 有多少个数x%f(x)==0
- #include <bits/stdc++.h>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LL long long
- #define inc(i,j,k) for(int i=j;i<=k;i++)
- #define dec(i,j,k) for(int i=j;i>=k;i--)
- #define gcd(i,j) __gcd(i,j)
- #define mem(i,j) memset(i,j,sizeof(i))
- const int N=2e5+;
- int rd, dfn[][][][];
- int fx, tot, a[];
- LL dp[][][][];
- // dp[f][w][s][m]
- // f为1说明不是上界 此时为第w位数
- // 剩下的数位总和为s 此时的数位总和%f(x)为m
- LL DFS(int f,int w,int s,int m) {
- if(w==) return (s==&&m==);
- // 所有位都枚举过了则w=0
- // 所有数位总和为fx则s=0
- // x%fx==0则m=0
- if(dfn[f][w][s][m]==rd) return dp[f][w][s][m];
- dfn[f][w][s][m]=rd;
- LL res=0LL;
- int l=max(,s-*(w-)), r=min((f ? :a[w]),s);
- for(int i=l;i<=r;i++) // 枚举当前位的数
- res+=DFS(f|(i<a[w]),w-,s-i,(m*+i)%fx);
- // 之前不是上界 或 当前位不是上界 则到目前为止都不达上界
- // 位数-1 剩余数位总和需减去当前位的数 更新余数
- return dp[f][w][s][m]=res;
- }
- LL solve(int x) {
- mem(dp,); mem(dfn,);
- int tot=;
- while(x) { a[++tot]=x%; x/=; }
- int all=tot*;
- LL res=0LL;
- for(fx=;fx<=all;fx++) // 枚举所有数位总和
- ++rd, res+=DFS(,tot,fx,);
- //printf("%lld\n",res);
- return res;
- }
- int main()
- {
- int _; scanf("%d",&_);
- inc(i,,_) {
- int l,r; scanf("%d%d",&l,&r); rd=;
- printf("Case %d: ",i);
- printf("%lld\n",solve(r)-solve(l-));
- }
- return ;
- }
nowcoder A hard problem /// 数位DP的更多相关文章
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- hiho1259 A Math Problem (数位dp)
题目链接:http://hihocoder.com/problemset/problem/1259 题目大意:g(t)=(f(i)%k=t)的f(i)的个数 求所有的(0-k-1)的g(i)的异或总值 ...
- 哈尔滨工程大学ACM预热赛 G题 A hard problem(数位dp)
链接:https://ac.nowcoder.com/acm/contest/554/G Now we have a function f(x): int f ( int x ) { if ( ...
- 『The Counting Problem 数位dp』
The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...
- UVA - 1640 The Counting Problem (数位dp)
题意:统计l-r中每种数字出现的次数 很明显的数位dp问题,虽然有更简洁的做法但某人已经习惯了数位dp的风格所以还是选择扬长避短吧(说白了就是菜啊) 从高位向低位走,设状态$(u,lim,ze)$表示 ...
- POJ2282:The Counting Problem(数位DP)
Description Given two integers a and b, we write the numbers between a and b, inclusive, in a list. ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- 2018.09.28 hdu5435A serious math problem(数位dp)
传送门 数位dp卡常题. 写了一发dfs版本的发现过不了233. 于是赶紧转循环版本. 预处理出f数组. f[i][j]f[i][j]f[i][j]表示前i位数异或和为j的方案数. 然后每次直接数位d ...
- 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)
链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
随机推荐
- 二 shell 基础
一 文件的 权限基础 文件有三类权限 user,group,other, 权限分为 r w x 代表数字分别为 4 2 1 修改权限命令 chmod 权限还有特殊权限,在执行的时候代表某一身 ...
- Codeforces 722E 组合数学 DP
题意:有一个n * m的棋盘,你初始在点(1, 1),你需要去点(n, m).你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终点的分数的数学期望是多少 ...
- 02 | 日志系统:一条SQL更新语句是如何执行的? 学习记录
<MySQL实战45讲>02 | 日志系统:一条SQL更新语句是如何执行的? 学习记录http://naotu.baidu.com/file/ad320c7a0e031c2d6db7b5a ...
- idea 查看类图
快捷键CTRL+H查看 利用idea快捷键查看hierarchy,效果如下 好处:可以看向上和向下的继承关系 缺点:只能看继承关系,不能看实现了哪些接口 在指定类右键查看diagram 也可以使用快捷 ...
- elasticsearch查询与sql对应关系
must: AND must_not:NOT should:OR
- C/C++ cJSON 使用实例
本文为转载 原文地址:http://blog.sina.com.cn/s/blog_4c0cb1c00102xg7j.html 使用说明:将cJSON.c.cJSON.h两个文件,拷贝到工程项目文件中 ...
- python基础:5.请编写一个函数实现将IP地址转换成一个整数。
如 10.3.9.12 转换规则为: 10 00001010 3 00000011 9 ...
- 关系型数据库---MYSQL---优化
1.为什么要进行SQL优化? 1.1 查询性能低 1.2 执行时间过长 1.3 等待时间过长 1.4 SQL写的太差(尤其是多表关联查询) 1.5 索引失效 1.6 服务器参数(缓存.线程数)设置不合 ...
- oracle11g笔记
安装 #!/bin/bash #安装oracle110203 pageDir="/opt/tools/oracle" bdFile="/tmp/bdFile.txt&qu ...
- Win7、Win10、Linux局域网文件共享
目录 1.WIN7系统局域网文件共享 2.WIN10系统局域网文件共享 3.Linux 局域网文件共享 WIN7系统局域网文件共享 步骤一: 打开网络和共享中心----更改高级共享设置 步骤二: 选择 ...