codeforces55D(离散化数位dp)
来源:互联网 发布:电脑压力测试软件 编辑:程序博客网 时间:2024/06/05 03:43
题意:问[L,R]10^18区间里面有多少个beautiful的数,beautiful数是每一位上的!0都被这个数整除。
思路:一个数能被它的所有数位上数整除的话,那么它就能把它的数位上的数的lcm整除。数位上的数最多是1-9,lcm最大为5 * 7 * 8 * 9 = 2520;
将前边的sum值保存一下前面的值,但是数会很大,dp数组存不下。我们要想好办法将这个数变小。因为lcm最大为2520,所以当sum的取模为2520,非常巧妙,。
按照定义,数字x为Beautiful :
x % LCM{digit[xi]} = 0
即 x % MOD % LCM{digit[xi]} = 0
#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int,int> P;typedef set<int>::iterator ITER;#define fi first#define se second#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x)const int maxn = 5e6 + 10;const int Mod = 1e9 + 7;const int N = 2;struct Matrix{Matrix(){clr(m,0);}int m[N][N];};Matrix Mul(Matrix a,Matrix b){Matrix c;for(int i = 0; i < N; i ++){for(int k = 0; k < N; k ++){if(a.m[i][k] == 0)continue;for(int j = 0; j < N; j ++)c.m[i][j] += a.m[i][k] * b.m[k][j] % Mod,c.m[i][j] %= Mod;}}return c;}ll Mul(ll a,ll b){a %= Mod;b %= Mod;ll ret = 0;while(b){if(b & 1){ret += a;if(ret > Mod)ret -= Mod;}b >>= 1;a = (a << 1) % Mod;}return ret;}Matrix pows(Matrix x,ll n){Matrix ret;for(int i = 0; i < 2; i ++)ret.m[i][i] = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}ll pows(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = ret * x % Mod;x = x * x % Mod;n >>= 1;}return ret;}ll powss(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}ll gcd(ll x,ll y){return y ? gcd(y,x % y):x;}ll lcm(ll x,ll y){return x /gcd(x,y) * y;}ll euler(int n){int ret = n;for(int i = 2; i * i<= n; i ++)if(n % i == 0){ret = ret / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ret = ret / n * (n - 1);return ret;}void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d = a;x = 1;y = 0;return;}ex_gcd(b,a % b,d ,y,x);y -=a/b * x;}inline int lowbit(int x){return x &(-x);}//void update(int x,int val){for(int i = x; x < maxn; i += lowbit(i))tree[i] += val;}//void get(int x){int ret = 0;for(int i = x; i > 0 ;i -= lowbit(i))ret += tree[i];return ret;}//int finds(int x){return fa[x] == x ? x : (fa[x] = finds(fa[x]));}int index[maxn];ll dp[20][2520][50];const int MOD = 2520;int bits[20];ll dfs(int pos,int sum,int lcms,bool flag){if(pos < 0)return sum % lcms == 0;if(!flag && dp[pos][sum][index[lcms]] != -1)return dp[pos][sum][index[lcms]];int up = flag ? bits[pos] : 9;ll ret = 0;for(int i = 0; i <= up; i ++){ll lcmss = lcms;if(i)lcmss = lcm(lcms,i);ret += dfs(pos - 1,(sum * 10 + i) % MOD,lcmss,flag && i == up);} if(!flag)dp[pos][sum][index[lcms]] = ret;return ret;}ll calc(ll n){int len = 0;while(n){bits[len ++] = n % 10;n /= 10;}return dfs(len - 1,0,1,true);}int main(){clr(dp,-1);int temps = 0;for(int i = 1; i <= 2520; i ++)if(2520 % i == 0)index[i] = temps ++;//cout << temps << endl;int Tcase;scanf("%d",&Tcase);while(Tcase --){ll n,m;scanf("%I64d%I64d",&n,&m);printf("%I64d\n",calc(m) - calc(n - 1));}return 0;}
阅读全文
0 0
- codeforces55D(离散化数位dp)
- codeforces55D(数位DP)
- codeforces55D. Beautiful numbers(数位dp+数论)
- BZOJ 3679 数位DP,离散化
- 数位DP(离散化+技巧)
- Beautiful numbers [数位dp][离散化]
- 【数位DP+离散化】Beautiful numbers CodeForces
- CodeForces 55D Beautiful numbers(数位dp&&离散化)
- CodeForces 55DBeautiful numbers 数位dp 离散化
- #51 D. Beautiful numbers (数位dp+离散化)
- CF_55D——Beautiful numbers(离散化数位DP)
- CodeForces55D
- 【cf】55d beautiful numbers【精妙的数位dp+离散化】
- codeforces 55D Beautiful numbers(数位dp+离散化)【模板】
- hdu3666 离散化dp
- Millionare_离散化&&DP
- 【dp+离散化】hdu 4028
- hdu5009 离散化+dp+优化
- hdu2160 母猪的故事(C语言)
- 【环境准备】centos 6.5 安装redis 源码安装
- 【PAT】【Advanced Level】1038. Recover the Smallest Number (30)
- 王者系列之springMVC
- c语言关键字简略解读
- codeforces55D(离散化数位dp)
- hdu6066RXD's date(水)
- Scala集合基本操作(1)
- Codeforces 651E Table Compression 拓扑序
- Java设计模式--工厂方法模式
- 数组中Arrays工具类方法的应用
- 计蒜客 踩蚂蚁(优先队列)
- 7、集合
- 1101. Quick Sort (25)快排