题目链接:点击查看

题目大意:求出一段区间 [l,r][l,r][l,r] 的数位和对 aaa 取模后为 000。更具体的,设 f(x)f(x)f(x) 为 xxx 的数位和,本题需要求出一对 [l,r][l,r][l,r] ,满足 ∑i=lrf(i)≡0(moda)\sum\limits_{i=l}^{r}f(i)\equiv0\pmod ai=l∑r​f(i)≡0(moda)

题目分析:假设一个上界为 inf=10kinf=10^{k}inf=10k,则一个数为 xxx 且 x<infx<infx<inf 时显然满足 f(x+inf)=f(x)+1f(x+inf)=f(x)+1f(x+inf)=f(x)+1

令 solve(l,r)=∑i=lrf(i)(moda)solve(l,r)=\sum\limits_{i=l}^{r}f(i)\pmod asolve(l,r)=i=l∑r​f(i)(moda),那么 solve(1,inf)=psolve(1,inf)=psolve(1,inf)=p

此时如果左右区间同时加一,得到 solve(2,inf+1)=solve(1,inf)−f(1)+f(1+inf)=p+1solve(2,inf+1)=solve(1,inf)-f(1)+f(1+inf)=p+1solve(2,inf+1)=solve(1,inf)−f(1)+f(1+inf)=p+1

如此递推下去不难看出 solve(1+k,inf+k)≡p+ksolve(1+k,inf+k)\equiv p+ksolve(1+k,inf+k)≡p+k

所以令 k=a−solve(1,inf)k=a-solve(1,inf)k=a−solve(1,inf) 就构造出一组答案为 [1+k,inf+k][1+k,inf+k][1+k,inf+k] 了

因为 aaa 的上限只有 1e181e181e18,所以取 inf=1e18inf=1e18inf=1e18 就可以了,当然更大的十的幂次也是可以的

最后 solve(1,1e18)solve(1,1e18)solve(1,1e18) 可以直接用数位 dp 求解

代码:

// Problem: Hack it!
// Contest: Virtual Judge - CodeForces
// URL: https://vjudge.net/problem/CodeForces-468C
// Memory Limit: 262 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const LL M=1e18;
LL mod;
LL dp[70][200][2];
int b[70],cnt;
LL dfs(int pos,int sum,bool limit) {if(pos==-1) return sum;if(dp[pos][sum][limit]!=-1) return dp[pos][sum][limit];int up=limit?b[pos]:9;LL ans=0;for(int i=0;i<=up;i++) ans=(ans+dfs(pos-1,sum+i,limit&&i==up))%mod;return dp[pos][sum][limit]=ans;
}
LL solve(LL x) {memset(dp,-1,sizeof(dp));cnt=0;while(x) {b[cnt++]=x%10;x/=10;}return dfs(cnt-1,0,1);
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);read(mod);LL k=mod-solve(M);printf("%lld %lld\n",1+k,M+k);return 0;
}

CodeForces - 468C Hack it!(构造+数位dp)相关推荐

  1. codeforces 401D. Roman and Numbers 数位dp

    题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. 1 #include<bits/stdc+ ...

  2. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  3. CodeForces - 1560F2 Nearest Beautiful Number (hard version)(二分+数位dp)

    题目链接:点击查看 题目大意:给出一个十进制数字 nnn 和一个约束 kkk,问大于等于 nnn 且满足不同的数位个数小于等于 kkk 的最小的数字是多少 题目分析:自己写的贪心太丑了,就不放上来丢人 ...

  4. HDU 3271 SNIBB(数位DP+构造)

    思路:数位DP+构造,先dp[i][j]表示i位总和为j的情况数,然后两种情况分别去进行数位DP,按高位往低位放去构造即可 代码: #include <cstdio> #include & ...

  5. 数位dp:Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    给出上下界,让你求出其中满足条件:不同的数字的数量不超过k个的数字的总和,答案模998244353,比如123里不同的数字个数为3,113里不同的数字个数为2,111里不同的数字个数为1. 跟普通的数 ...

  6. CodeForces - 628D Magic Numbers(数位dp)

    题目链接:点击查看 题目大意:首先规定一个不含前导零的数字如果满足: 从最高位开始,偶数位置全为 ddd 从最高位开始,奇数位置不能出现 ddd 则称该数字为 d−magicd-magicd−magi ...

  7. CodeForces 258B Little Elephant and Elections 数位DP

    前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #i ...

  8. Codeforces 55D Beautiful numbers (数位DP)

    题意:有T组询问,每次询问区间[l, r]中的beautiful number有多少.beautiful number是指这个数可以被组成它的数字整除.例如15是beautiful number,因为 ...

  9. codeforces 628D. Magic Numbers 数位dp

    题目链接 给两个数m, d. 两个数a, b. a, b长度小于2000, 长度相等.求在a, b之间的数x, x%m==0, 并且从高位往低位数, 奇数位的数全部不等于d, 偶数为的数全都等于d, ...

最新文章

  1. USB OTG原理+ ID 检测原理
  2. alluxio源码解析-层次化存储(4)
  3. Maven安装与配置详解(Win10)
  4. Dijkstra算法实现
  5. golang 导入自定义包_二、Go基本命令及定制自定义第三方包
  6. 递归求解全排列问题以及八皇后问题
  7. 差分编码 Differential Encoding
  8. Excel单元格提取数字
  9. 时差怎么理解_懂的人自然懂,不懂的人再多解释也有时差
  10. winmerge多个文件夹生成html,功能强大的文件、文件夹比对工具-WinMerge使用教程
  11. 写给20几岁的女孩、男孩
  12. 小程序页面如何直接调用云存储中的图片
  13. Android 面试题中高级
  14. 物联网开源工具Unik:用Unikernel提高连接安全性
  15. 如何给TSM磁带机添加新磁带
  16. vmware虚拟机动态添加硬盘识别不了新增加的硬盘
  17. Windows系统的电脑网卡,生产日期等信息查询
  18. win7系统清除卸载软件后残留的图标技巧--win10专业版
  19. 微信内置浏览器清除缓存解决方案(实测有效)
  20. DirectX SDK各版本下载地址备份

热门文章

  1. 企业防火墙代替路由器_「防火墙技术连载」基础知识篇 1、什么是防火墙
  2. 引入OAuth2的主要目的
  3. seata的部署和集成
  4. 使用Docker-容器命令介绍
  5. xml方式实现aop-通知的种类
  6. maven依赖管理的概念
  7. 消息消费要注意的细节
  8. 对文本的内容进行排序
  9. HDFS的namenode和datanode
  10. Request_获取请求行数据_方法介绍