[디지털 DP] codeforces 55D & FZU chriswho

2382 단어 dpHDU
sign...어려운 디지털 DP...
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1005
#define maxm 200005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='
')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head const int tt = 2520; LL dp[20][3000][50]; int x[50], mpp[3000]; int s[50]; int cnt; void init(void) { cnt = 0; for(int i = 1; i <= tt; i++) if(tt % i == 0) x[++cnt] = i, mpp[i] = cnt; memset(dp, -1, sizeof dp); } LL dfs(int pos, int sum, int lcm, int limit) { if(pos == 0) { if(sum % x[lcm] == 0) return 1; else return 0; } if(dp[pos][sum][lcm] != -1 && !limit) return dp[pos][sum][lcm]; lcm = x[lcm]; int b = (limit ? s[pos] : 9); LL ans = 0; for(int i = 0; i <= b; i++) { int nlcm; if(!i) nlcm = lcm; else nlcm = lcm / gcd(lcm, i) * i; ans += dfs(pos - 1, (sum * 10 + i) % tt, mpp[nlcm], limit && i == b); } if(!limit) dp[pos][sum][mpp[lcm]] = ans; return ans; } LL calc(LL t) { int size = 0; while(t) { s[++size] = t % 10; t /= 10; } return dfs(size, 0, 1, 1); } int main(void) { int _; LL a, b; init(); while(scanf("%d", &_)!=EOF) { while(_--) { scanf("%I64d%I64d", &a, &b); printf("%I64d
", calc(b) - calc(a - 1LL)); } } return 0; }

좋은 웹페이지 즐겨찾기