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 ...
随机推荐
- 高级定时器-setTimeout()、setInterval()、链式setTimeout()
使用 setTimeout()和 setInterval()创建的定时器可以用于实现有趣且有用的功能.执行时机是不能保证的,因为在页面的生命周期中,不同时间可能有其他代码在控制 JavaScript ...
- 安卓构架组件——概述 Android Architecture Components
谷歌官文文档地址:https://developer.android.google.cn/topic/libraries/architecture 安卓构架组建是库的集合:帮助你设计健壮的.易测试的. ...
- openstack stein部署手册 1. 准备
# 控制节点与计算节点,分别指定静态解析 /etc/hosts 192.168.123.201 controller 192.168.123.202 compute01 # 控制节点与计算节点,分别安 ...
- spring的@Value注解使用
https://blog.csdn.net/woheniccc/article/details/79804600 昨天看到了springMVC的controller中的属性使用了@value注解,并且 ...
- Linux学习笔记之磁盘与文件系统的管理
三.Linux磁盘与文件系统的管理 MBR扇区(512B) 磁盘的分区组成 Boot sector 扇区(用来装引导程序) Super block 记录inode与Block的信息 Inod ...
- 在vCenter上创建新用户 (适用版本6.0)
- 没有dockerfile的情况下如何查看docker的镜像信息
前言 参考资料 https://baijiahao.baidu.com/s?id=1564406878758073&wfr=spider&for=pc 很实用的功能哈.. 步骤 1.先 ...
- 前端学习(三十八)vue(笔记)
Angular+Vue+React Vue性能最好,Vue最轻=======================================================Angular ...
- java类的加载与初始化
https://blog.csdn.net/u013349237/article/details/71076617 1在命令行启动虚拟机jvm进行加载, 2用class.forname()方法进行动态 ...
- websock(AMQ)通信-前端
服务端和客户端之间的通信 前端开发经常会依赖后端,那么如果后端服务器还没做好推送服务器,那么前端该如何呢.最简单的就是自己模拟一个服务器,用node来搭建,这边只简单介绍搭建的过程 node搭建服务器 ...