当前位置:网站首页>Luogu p4127 [ahoi2009] similar distribution problem solution
Luogu p4127 [ahoi2009] similar distribution problem solution
2022-07-06 07:29:00 【q779】
Luogu P4127 [AHOI2009] The same kind of distribution Answer key
Topic link :P4127 [AHOI2009] The same kind of distribution
The question :
Give two numbers a , b a,b a,b , Find out [ a , b ] [a,b] [a,b] The sum of the numbers in can divide the number of the original number .
For all the data , 1 ≤ a ≤ b ≤ 1 0 18 1 ≤ a ≤ b ≤ 10^{18} 1≤a≤b≤1018.
Tomorrow is the final exam, not only do not review, but also brush questions qwqqwqwq
Because we can't know the digit sum of each number exactly
But we know they must be [ 1 , 9 × l ] [1,9 \times l] [1,9×l] Within the scope of ( l l l Is the longest digit )
We enumerate all digit sums and calculate each digit and the corresponding answer
Add up these answers to [ 0 , x ] [0,x] [0,x] The answer
[ a , b ] [a,b] [a,b] The answer of can be split into two questions [ 0 , b ] [0,b] [0,b] and [ 0 , a − 1 ] [0,a-1] [0,a−1]
And here a , b ≤ 1 0 18 a,b \le 10^{18} a,b≤1018 , The direct press in state is obviously unacceptable
So we can consider recording the digits of the current enumeration and the numbers in the sense
Convenience , We call the sum of digits of the current enumeration m m m
set up f i , j , k f_{i,j,k} fi,j,k It means that only the former i i i digit ( Including leading zeros ), front i i i The sum of digits is j j j , Current digital analog m m m by k k k The answer is
It's not hard to find out
f i , j , k = ∑ 0 ≤ d ≤ t f i + 1 , j + d , ( k × 10 + d m o d m ) f_{i,j,k} = \sum_{0 \le d \le t} f_{i+1,j+d,\left(k\times 10 + d \, \bmod \, m\right)} fi,j,k=0≤d≤t∑fi+1,j+d,(k×10+dmodm)
among t t t For the first time i + 1 i+1 i+1 High bit limit
The search and writing method can be much simpler
Time complexity O ( 9 3 × l 4 ) O(9^3\times l^4) O(93×l4)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)()
int len,num[25],f[25][205][205];
int dfs(int u,int sum,int st,int limit,int p)
{
if(u>len)
{
if(!sum)return 0;
return st==0&&sum==p;
}
if(!limit&&f[u][sum][st]!=INF)
return f[u][sum][st];
int up=limit?num[len-u+1]:9;
int res=0;
for(int i=0; i<=up; i++)
res+=dfs(u+1,sum+i,(10*st+i)%p,limit&&i==up,p);
return limit?res:f[u][sum][st]=res;
}
int solve(int x)
{
int res=0; len=0;
while(x) num[++len]=x%10,x/=10;
for(int m=1; m<=9*len; m++)
{
memset(f,0x3f,sizeof(f));
res+=dfs(1,0,0,1,m);
}
return res;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
int l,r; cin >> l >> r;
cout << solve(r)-solve(l-1) << '\n';
return 0;
}
It's okay anyway whk For the time being
Reprint please explain the source
边栏推荐
- Typescript interface properties
- word中把帶有某個符號的行全部選中,更改為標題
- Chrome view page FPS
- Simulation of Teman green interferometer based on MATLAB
- Get/post/put/patch/delete meaning
- Redis builds clusters
- Jerry needs to modify the profile definition of GATT [chapter]
- TypeScript接口与泛型的使用
- C # create database connection object SQLite database
- Path analysis model
猜你喜欢
![[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code](/img/93/ec9de907cae4714038bb5f95aba52b.png)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code

Path analysis model

qt颜色与字符串、uint相互转换
![If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]](/img/d6/92ad1c6f84415de6ab0dfd16cd6073.png)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]

How are the open source Netease cloud music API projects implemented?
![If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]](/img/57/12a97ab3d2dabfaf06bbe1788450cf.png)
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]

navicat如何导入MySQL脚本

杰理之BLE【篇】

NiO programming introduction

Solution to the problem of breakthrough in OWASP juice shop shooting range
随机推荐
Rust language - receive command line parameter instances
C intercept string
#systemverilog# 可综合模型的结构总结
navicat如何导入MySQL脚本
word设置目录
The way to learn go (II) basic types, variables and constants
Uni app practical project
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
Cookie Technology & session Technology & ServletContext object
word中把带有某个符号的行全部选中,更改为标题
SSM learning
On the world of NDK (2)
[MySQL learning notes 29] trigger
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
Go learning --- use reflection to judge whether the value is valid
【JDBC】快速入门教程
Yield method of tread
Twelve rules for naming variables
杰理之蓝牙设备想要发送数据给手机,需要手机先打开 notify 通道【篇】