Codeforces Round #287 (Div. 2) D. The Maths Lecture(数位dp)
来源:互联网 发布:淘宝卖家体检中心链接 编辑:程序博客网 时间:2024/06/05 00:31
题意:
N≤1000位数字,求这样的数字x有多少个
x>0且x没有前导0,存在一个x的后缀y>0,满足y%k=0,k≤100
分析:
赤果果的数位dp
因为有后缀,显然倒着构造这个数就可以了
f[i][mod][ok]:=从第n位到第i位,模数为mod,是否含有后缀>0且mod=0,的数字数
注意处理后导0,倒着构造就是前导0了
并且这个数非负,也就是说第1位,倒着构造就是n位∈[1,9],不能取0
还有添加一个数的时候不是(mod∗10+d)%k,而是(d∗10i+mod)%k,没注意这个想了半天
代码:
//// Created by TaoSama on 2016-02-12// Copyright (c) 2016 TaoSama. All rights reserved.//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, k, m;inline void add(int& x, int y) { if((x += y) >= m) x -= m;}int f[N][100][2], power[N];int dfs(int i, int mod, bool ok, bool notSuf) { if(i == n) return ok; int& ret = f[i][mod][ok]; if(~ret) return ret; ret = 0; for(int d = i == n - 1; d <= 9; ++d) { int nxtMod = (d * power[i] % k + mod) % k; int nxtOk = ok | (notSuf && d == 0 ? 0 : nxtMod == 0); add(ret, dfs(i + 1, nxtMod, nxtOk, notSuf && d == 0)); } return ret;}int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); while(scanf("%d%d%d", &n, &k, &m) == 3) { memset(f, -1, sizeof f); power[0] = 1; for(int i = 1; i <= n; ++i) power[i] = power[i - 1] * 10 % k; printf("%d\n", dfs(0, 0, 0, 1)); } return 0;}
0 0
- Codeforces Round #287 (Div. 2) D. The Maths Lecture (数位dp)
- Codeforces Round #287 (Div. 2) D. The Maths Lecture(数位dp)
- Codeforces 507D (Round #287(div.2))D. The Maths Lecture【数位DP】
- Codeforces Round #287 (Div. 2) D. The Maths Lecture 数位dp
- Codeforces Round #287 (Div. 2)-D. The Maths Lecture(数位dp)
- codeforces--507D--The Maths Lecture(数位dp)
- CF #287 div2 D The Maths Lecture 数位DP
- The Maths Lecture - CodeForces 507 D dp
- Codeforces507D 数位dp The Maths Lecture
- Codeforences #287 div2 D. The Maths Lecture
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
- Codeforces Round #157 (Div. 2)D(数位DP+组合数)
- 507D The Maths Lecture
- Codeforces Round #228 Div.1 D 数位dp+数学
- Codeforces Round #265 (Div. 2)E(数位dp)
- Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树) 好题
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树)
- 移动端第三方登录(微信)java验证并获取用户信息
- 326. Power of Three
- fluentd收集mapreduce分布式环境下的用户日志
- 231. Power of Two
- 华为机试题:小明的筷子
- Codeforces Round #287 (Div. 2) D. The Maths Lecture(数位dp)
- 从零开始搭建opencv3.0交叉编译环境(Ubuntu14.04+eclipse)(二)
- 【吐槽】颓废良久了
- 232. Implement Queue using Stacks
- 广播(Broadcast)的简单用法
- 第一章 关于语言,对象
- MySql 5.7安装(随机密码,修改默认密码)
- 二分查找
- struts2编写自定义拦截器filter