CodeForces - 468C Hack it!(构造+数位dp)

Posted Frozen_Guardian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 468C Hack it!(构造+数位dp)相关的知识,希望对你有一定的参考价值。

题目链接:点击查看

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

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

s o l v e ( l , r ) = ∑ i = l r f ( i ) ( m o d a ) solve(l,r)=\\sum\\limits_{i=l}^{r}f(i)\\pmod a solve(l,r)=i=lrf(i)(moda),那么 s o l v e ( 1 , i n f ) = p solve(1,inf)=p solve(1,inf)=p

此时如果左右区间同时加一,得到 s o l v e ( 2 , i n f + 1 ) = s o l v e ( 1 , i n f ) − f ( 1 ) + f ( 1 + i n f ) = p + 1 solve(2,inf+1)=solve(1,inf)-f(1)+f(1+inf)=p+1 solve(2,inf+1)=solve(1,inf)f(1)+f(1+inf)=p+1

如此递推下去不难看出 s o l v e ( 1 + k , i n f + k ) ≡ p + k solve(1+k,inf+k)\\equiv p+k solve(1+k,inf+k)p+k

所以令 k = a − s o l v e ( 1 , i n f ) k=a-solve(1,inf) k=asolve(1,inf) 就构造出一组答案为 [ 1 + k , i n f + k ] [1+k,inf+k] [1+k,inf+k]

因为 a a a 的上限只有 1 e 18 1e18 1e18,所以取 i n f = 1 e 18 inf=1e18 inf=1e18 就可以了,当然更大的十的幂次也是可以的

最后 s o l v e ( 1 , 1 e 18 ) 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)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces hack

Hack It

HDU 6313: Hack it

HDU - 6313 Hack It(构造)

HDU 6313 Hack It (循环加群构造)

(You Can Hack It, Architecture and Design) => { Dependency Injection; }