CodeForces 55D

深碍√TFBOYSˉ_ 2023-08-17 16:37 250阅读 0赞

题意略。

思路:

本题可以说是醉翁之意不在酒了。要解开本题有几个关键点:

1.意识到数X = An An-1 An-2 An-3 …. A2 A1能被{An,An-1,An-2,….,A1}这n个数整除的充要条件是lcm(An,An-1,An-2,….,A1) | X。

2.要知道1~9这9个数进行组合最大的lcm是2520,而且组合出的lcm个数有限,最多48个。

3.我们为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只需记录它对2520的模即可。

因为所有的lcm都是2520的因子,所以我们用mod 2520的值来表示当前已经摆好组成的值,到了最后pos == -1的时候,我们再把当前的值mod 当前若干数位组合好的lcm,看看mod lcm 是不是等于0。

如果等于0,则是合格的解。

4.不用考虑前导0的影响。因为在 !limit 的前提下 1111[ ][ ] 与 [ ][ ]是一样的,对当前组合好的lcm不会产生影响。

代码附上:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn0 = 20;
  5. const int maxn1 = 3000;
  6. const int maxn2 = 50;
  7. const int mod = 2520;
  8. int a[maxn0],tail,T;
  9. LL dp[maxn0][maxn2][maxn1],l,r;
  10. int mp[maxn1],store[maxn1],t;
  11. int gcd(int a,int b){
  12. return b == 0 ? a : gcd(b,a % b);
  13. }
  14. int lcm(int a,int b){
  15. int d = gcd(a,b);
  16. return a / d * b;
  17. }
  18. void process(LL x){
  19. tail = 0;
  20. while(x){
  21. a[tail++] = int(x % 10);
  22. x /= 10;
  23. }
  24. }
  25. LL dfs(int pos,int Lcm,int remainder,bool limit){
  26. if(pos == -1 && remainder % store[Lcm] == 0) return 1;
  27. if(pos == -1 && remainder % store[Lcm] != 0) return 0;
  28. if(!limit && dp[pos][Lcm][remainder] != -1) return dp[pos][Lcm][remainder];
  29. int up = limit ? a[pos] : 9;
  30. LL ret = 0;
  31. for(int i = 0;i <= up;++i){
  32. int truelcm = store[Lcm];
  33. int ntruelcm = i == 0 ? truelcm : lcm(truelcm,i);
  34. int nlcm = mp[ntruelcm];
  35. int nremainder = (remainder * 10 + i) % mod;
  36. ret += dfs(pos - 1,nlcm,nremainder,limit && i == up);
  37. }
  38. if(!limit) dp[pos][Lcm][remainder] = ret;
  39. return ret;
  40. }
  41. void prepare(){
  42. int total = (1<<9);
  43. for(int i = 1;i < total;++i){
  44. int temp = 1;
  45. for(int j = 0;j < 9;++j){
  46. if(((i>>j) & 1) == 0) continue;
  47. temp = lcm(temp,(j + 1));
  48. }
  49. store[t++] = temp;
  50. }
  51. sort(store,store + t);
  52. t = unique(store,store + t) - store;
  53. for(int i = 0;i < t;++i){
  54. mp[store[i]] = i;
  55. }
  56. }
  57. int main(){
  58. prepare();
  59. memset(dp,-1,sizeof(dp));
  60. scanf("%d",&T);
  61. while(T--){
  62. scanf("%I64d%I64d",&l,&r);
  63. process(l - 1);
  64. LL ans0 = dfs(tail - 1,0,0,true);
  65. process(r);
  66. LL ans1 = dfs(tail - 1,0,0,true);
  67. printf("%I64d\n",ans1 - ans0);
  68. }
  69. return 0;
  70. }

转载于:https://www.cnblogs.com/tiberius/p/11437488.html

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces1214D

    [CodeForces1214D][] 这个题据我所知有两种比较优秀的做法. 第一种是\\(DP\\)统计每个点的路径数,然后找出必经点,再从必经点开始\\(bfs\\

    相关 CodeForces1208D

    CodeForces1208D 也是挺吓人的一道题,我一开始以为给的是每个数字前比它小的数字有几个,然后我就苦苦看不懂样例... 然后我冷静了一下,重新分析,读题,发现

    相关 Codeforces 496D

    题意 -------------------- 进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛