nowcoder A hard problem /// 数位DP
2024-09-05 23:41:12
题目大意:
称一个数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 ;
}
最新文章
- iOS为真机调试增加scribble来定位野指针
- javascript里面foreach遍历函数介绍,以及跟jquery里面each的区别
- Ajax全解
- Silverlight动画学习笔记(三):缓动函数
- Matlab与科学计算的基本运算
- POJ1159 Palindrome(dp)
- Applovin Interview (面经)
- Linux 常 用 命 令
- Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server ";xxxxxxx";.
- js实现两种实用的排序算法——冒泡、快速排序
- Linux input子系统 io控制字段【转】
- ucloud mysql
- 超实用的JavaScript代码段 Item5 --图片滑动效果实现
- 02:安装 Kerberos
- MapReduce实现PageRank算法(稀疏图法)
- Xamarin Android ListView 控件使用
- curl模拟访问已经存在的cookie
- vue 环境报错 chromedriver@2.44.1 install: `node install.js`
- P3379 【模板】最近公共祖先(LCA)(树链剖分)版
- SGU刷题之路开启