hdu4507 恨7不成妻 麻烦的数位dp

花了近一个下午才想明白如何用子状态的平方和更新自状态的平方和,这个题恶心到俺了。。。。
hdu4507
可以看看https://blog.csdn.net/LightningUZ/article/details/90619068
写得超级详细,但是代码是伪代码。

如果一个整数符合下面3个条件之一,那么我们就说这个整数和7有关——
  1、整数中某一位是7;
  2、整数的每一位加起来的和是7的整数倍;
  3、这个整数是7的整数倍;
  现在问题来了:吉哥想知道在一定区间内和7无关的数字的平方和。

在dfs过程中,
对于条件1,肯定是这样:

	if(i==7) continue;

对于条件2和条件3,大概是这样:

	if(pos < 0){
		if(位和 % 7==0 || 数和 % 7 == 0) return 0;
		else return 1;
	}

所以dp的维数就是pos, 位和, 数和共三维。
自然dfs也要携带上位和与数和。

dfs(int pos, bool limit, ll bitmod, ll premod)
//bitmod是位和对7模的结果, premod是数和对7模的结果

上述不是难点, 难点在于这个题不是求满足题意的数的个数, 而是要求满足题意的数的平方和。
我们类比个数是如何更新的:

tmp = dfs(...);//如果dfs返回的是个数
ans += tmp;//子状态的答案更新本状态的答案
.....
return ans;

从简单向难考虑,如果题目问我们只是和, 而不是平方和,我们该如何更新?
比如说13 x x x这个数,考虑 x x x所在位向第二位更新,假设第二位是2,如果x中满足题意的数的个数是 x . c n t x.cnt x.cnt,然后这些个数的和是 x . s u m x.sum x.sum ,那么 2 x 2x 2x这个数中满足题意的数的个数肯定要加上 x . c n t x.cnt x.cnt,假设这 c n t cnt cnt个数中有1-9,那么 2 x 2x 2x新加上的满足题意的数肯定是21-29,也就是x中的每一个满足题意的数加上20,于是这就可以更新 2 x . s u m 2x.sum 2x.sum
2 x . s u m + = x . s u m + x . c n t ∗ 这 一 位 是 几 ∗ 1 0 p o s 2x.sum += x.sum + x.cnt * 这一位是几 * 10^{pos} 2x.sum+=x.sum+x.cnt10pos
我们设 h i g h high high= 1 0 p o s ∗ 这 一 位 是 几 10^{pos} * 这一位是几 10pos
经过各种玄学思考与类比,我们发现平方和这样更新:
a n s . s u m 2 = ∑ ( t m p . s u m 2 + 2 ∗ h i g h ∗ t m p . s u m + h i g h 2 ∗ t m p . c n t ) ans.sum^2 = \sum(tmp.sum^2 + 2 * high * tmp.sum + high^2 * tmp.cnt) ans.sum2=(tmp.sum2+2hightmp.sum+high2tmp.cnt)
(想不明白就看上边的博客吧,自认讲不明白 o r z orz orz
那么也就是说平方和要用和与个数共同更新, 所以我们把dp设成结构体同时对三个数保存与更新即可,初始化暴力初始化即可。
这个题把一切可mod的地方都mod,不然就很轻易地wawawa
c o d e code code

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

ll a[25];
struct Node{
	ll cnt, sum, sum_2;
	Node(ll x = 0, ll y = 0, ll z = 0):cnt(x), sum(y), sum_2(z){};
}dp[25][7][7];

ll pow(ll x){
	ll ans = 1, res = 10;
	while(x){
		if(x&1) ans = ans * res % mod;
		res = res * res % mod;
		x >>= 1;
	}
	return ans % mod;
}

inline update(Node &ans, Node &tmp, ll &h){	
	ans.cnt = (ans.cnt + tmp.cnt) % mod;
	ans.sum = (ans.sum + (tmp.cnt * h % mod + tmp.sum) % mod) % mod;
	ans.sum_2 = (ans.sum_2 + (tmp.sum_2 + ((2 * h) % mod * tmp.sum) % mod + ((h * h) % mod * tmp.cnt) % mod) % mod) % mod;
}

Node dfs(int pos, bool limit, ll bitmod, ll premod){
	if(pos < 0){
		if(!bitmod%7 || !premod%7) return Node(0, 0, 0);
		else return Node(1, 0, 0);
	}
	if(!limit && dp[pos][bitmod][premod].cnt != -1) return dp[pos][bitmod][premod];
	ll up = limit ? a[pos]:9;
	Node ans;
	for(ll i = 0; i <= up; i++){
		if(i==7) continue;
		Node tmp = dfs(pos - 1, limit && i == up, (bitmod + i) % 7, (premod * 10 % 7 + i) % 7);
		ll h = i * pow(pos) % mod;//这里写了一个求10的快速幂
		update(ans, tmp, h);
	}
	if(!limit) dp[pos][bitmod][premod] = ans;
	return ans;
}

void init(){
	for(int i = 0; i < 25; i++){
		for(int j = 0; j < 7; j++)
			for(int k = 0; k < 7; k++){
				dp[i][j][k] = Node(-1, 0, 0);
			}
	}
}

ll solve(ll x){
	ll pos = 0;
	while(x){
		a[pos++] = x % 10;
		x /= 10;
	}
	return dfs(pos - 1, true, 0, 0).sum_2 % mod;
}
int main(){
	ll n;
	ll l, r;
	scanf("%lld", &n);
	init();
	while(n--){
		scanf("%lld%lld", &l, &r);
		printf("%lld\n", (solve(r) - solve(l - 1) + mod) % mod);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值