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 ...
随机推荐
- jar 启动脚本
前段时间用springboot做项目后,每次重新发布都好麻烦, 所以写了个脚本来配合jenkins 发布: #!/bin/bash APP_NAME=application.jar function ...
- 使用纯php构建一个简单的PHP服务器
使用原生PHP构建一个简单的PHPWeb服务器 1.目录机构 webserver --src -- Response.php -- Server.php -- Request.php -- vendo ...
- 2018-2-13-win10-UWP-RSS阅读器
title author date CreateTime categories win10 UWP RSS阅读器 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 1 ...
- egrep 或 多个连续字符测数字
.TXT 4-6是ABC 或者 4-6是 0-9
- Sass函数:unit()函数
unit() 函数主要是用来获取一个值所使用的单位,碰到复杂的计算时,其能根据运算得到一个“多单位组合”的值,不过只充许乘.除运算: >> unit(100) "" & ...
- python 中 len()和range()
https://blog.csdn.net/qq_36357820/article/details/77850841
- FreeBSD虚拟机——小折腾
最近,突然想起来Linux了,因为前段时间接触了DOS命令,提高了自己的工作效率,这会想再温习下Linux的CLI,无奈windows下unix命令,水土不服,因此想尝试虚拟机,虚拟机软件无非vmwa ...
- nyoj 78:圈水池 【凸包入门】
题目链接 将所有点按从左至右顺序排序,然后将所有点先从左到右扫描再从右到左扫描,逐渐将凸包轮廓“勾勒”出来 (凸包轮廓满足,轮廓上连续的三个点按先后顺序给出的话呈逆时针方向) 最后删去一个重复的起(终 ...
- poj 3258:River Hopscotch(二分)
题目链接 L为N+2块石子中最右边石子位置,0最左,M为可移除块数,求移除后相邻石子可达到的最大距离. #include<iostream> #include<cstdio> ...
- 【leetcode】1018. Binary Prefix Divisible By 5
题目如下: Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted a ...