首页 > 其他 > 详细

CodeForces 55D Beautiful numbers (数位DP)

时间:2016-10-13 23:58:27      阅读:398      评论:0      收藏:0      [点我收藏+]

题意:给求给定区间中该数能整除每一位的数的数量。

析:dp[i][j][k] 表示前 i 位,取模2520为 j,最小倍数是 k,但是这样,数组开不下啊,那怎么办呢,其实,0-9的最小公倍数的不同各类并没有那么多,

其实就48种,所以我们可以给这48个一个编号,然后就能开出来了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 100;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
//const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline int gcd(int a, int b){ return b ? gcd(b, a%b) : a; }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
const int all[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 56, 60, 63, 70, 72, 84, 90, 105, 120, 126, 140, 168, 180, 210, 252, 280, 315, 360, 420, 504, 630, 840, 1260, 2520};
LL dp[25][2600][50];
int a[25];
map<int, int> id;
map<int, int> mp;
void init(){
    for(int i = 0; i < 48; ++i)
        mp[i] = all[i], id[all[i]] = i;
}

LL dfs(int pos, int val, int num, bool ok){
    if(!pos)  return !(val % mp[num]);
    LL &ans = dp[pos][val][num];
    if(!ok && ans >= 0)  return ans;

    LL res = 0;
    int n = ok ? a[pos] : 9;
    for(int i = 0; i <= n; ++i)
        if(!i)  res += dfs(pos-1, val*10%2520, num, ok && i == n);
        else  res += dfs(pos-1, (val*10+i)%2520, id[lcm(mp[num], i)], ok && i == n);

    return ok ? res : ans = res;
}

LL solve(LL n){
    int len = 0;
    while(n){
        a[++len] = n % 10;
        n /= 10;
    }
    return dfs(len, 0, 0, true);
}

int main(){
    init();
    memset(dp, -1, sizeof dp);
    int T;  cin >> T;
    LL n, m;
    while(cin >> m >> n){
        cout << solve(n) - solve(m-1) << endl;
    }
    return 0;
}

 

CodeForces 55D Beautiful numbers (数位DP)

原文:http://www.cnblogs.com/dwtfukgv/p/5958393.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!