标签:
题目大意:一个数是幸运数当且仅当这个数能整除所有位数,求[a,b]有多少幸运数
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int maxa = 20; const int mod = 2520; long long dp[maxa][1<<8][mod]; #define LL long long //当前位置,限制,有哪些数,模 int num[maxa]; LL dfs(int pos, int limit, int val, int numb){ if(pos == 0){ for(int i = 2; i < 10; i++){ if((1<<(i-2)) & val){ if(numb % i) return 0; } }return 1; } if(!limit && dp[pos][val][numb] != -1) return dp[pos][val][numb]; int nn = limit?num[pos]:9; LL res = 0; for(int i = 0; i <= nn; i++){ res += dfs(pos-1, limit & (i ==nn), i<2?val:val|(1<<(i-2)), (numb*10+i)%mod); } if(!limit) dp[pos][val][numb] = res; return res; } LL ANS(LL n){ int leng = 0; while(n){ num[++leng] = n %10; n /= 10; } return dfs(leng, 1, 0, 0); } int main(){ int t; LL a, b; memset(dp, -1, sizeof(dp)); scanf("%d", &t); while(t--){ cin>>a>>b; if(a > b)swap(a, b); cout<<ANS(b) - ANS(a-1)<<endl; } }
标签:
原文地址:http://www.cnblogs.com/icodefive/p/4437999.html