【洛谷】P1822 魔法指纹 分段打表

P1822 魔法指纹

题目大意

定义 \(magic(n)\) 为:将n按十进制顺序写下来,依次对相邻两个数写下差的绝对值,去掉前导 \(0\),为 \(magic(n)\)。若 \(n\) 为一位数,则 \(magic(n)=n\)

定义幸运数为: 重复对 \(n\)\(magic()运算\),如果最后可以变为 \(7\),那么 \(n\) 就是一个幸运数。

给定区间 \([l,r]\) 问这个区间中有多少个幸运数。

题解

\(l,r\)的范围均为 \([1,10^9]\)

暴力求解肯定不行。

我们将 \([1,10^9]\) 分成 \(1000\) 段,分别将这 \(1000\) 段中的幸运数的个数计算好。

直接初始化到一个数组中,然后对于区间 \([1,r]\),我们最多只需暴力求解长度为 \(1000000\) 的区间。

代码

//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int seed = 233;
const int N = 2e5 + 10;

int arr[2000] = { 0, 2045, 1044, 750, 357, 316, 316, 358, 749, 1044, 1474, 866, 1044, 303, 220, 154, 115, 84, 125, 346, 927, 378, 558, 750, 221, 204, 154, 118, 134, 146, 382, 110, 270, 303, 357, 176, 204, 168, 128, 89, 75, 85, 126, 298, 221, 316, 176, 220, 168, 88, 69, 69, 88, 168, 220, 176, 316, 221, 298, 126, 85, 75, 89, 128, 168, 204, 176, 357, 303, 270, 111, 382, 145, 134, 118, 154, 204, 222, 749, 558, 378, 927, 347, 124, 84, 115, 154, 220, 303, 1044, 866, 2022, 754, 224, 71, 86, 115, 168, 298, 558, 1474, 626, 558, 118, 81, 51, 33, 19, 30, 316, 1461, 866, 1044, 303, 220, 154, 115, 84, 125, 346, 927, 110, 558, 94, 221, 96, 77, 48, 32, 26, 60, 74, 270, 116, 66, 95, 204, 118, 83, 55, 29, 50, 126, 118, 77, 50, 76, 113, 168, 57, 43, 44, 88, 108, 79, 40, 41, 44, 91, 98, 85, 41, 89, 69, 81, 34, 23, 30, 21, 80, 67, 136, 145, 74, 64, 51, 21, 22, 24, 35, 53, 489, 347, 75, 61, 51, 24, 13, 28, 172, 79, 1143, 754, 47, 45, 51, 36, 15, 20, 229, 924, 240, 240, 298, 118, 69, 51, 28, 43, 63, 585, 866, 328, 303, 113, 77, 51, 35, 25, 62, 375, 378, 558, 750, 221, 204, 154, 118, 134, 146, 382, 67, 194, 303, 84, 176, 96, 81, 49, 32, 25, 47, 98, 298, 126, 69, 95, 220, 108, 52, 38, 43, 57, 168, 113, 76, 50, 77, 118, 126, 50, 48, 69, 128, 118, 60, 40, 34, 29, 96, 74, 156, 97, 134, 76, 77, 34, 27, 23, 26, 55, 162, 193, 124, 54, 69, 51, 23, 23, 35, 37, 311, 315, 224, 35, 58, 51, 22, 28, 72, 277, 25, 78, 118, 168, 65, 58, 35, 27, 14, 14, 382, 92, 116, 220, 101, 69, 53, 33, 39, 32, 110, 558, 94, 221, 96, 77, 48, 32, 26, 60, 110, 270, 303, 357, 176, 204, 168, 128, 89, 75, 34, 83, 118, 221, 64, 176, 113, 109, 42, 27, 38, 52, 108, 220, 95, 69, 126, 298, 98, 47, 35, 54, 69, 168, 96, 76, 59, 65, 194, 110, 51, 66, 74, 118, 101, 60, 44, 30, 47, 138, 32, 60, 75, 84, 65, 77, 52, 21, 25, 33, 24, 41, 47, 71, 52, 69, 56, 34, 35, 16, 23, 47, 91, 118, 115, 52, 45, 35, 16, 15, 170, 64, 65, 113, 154, 65, 61, 32, 32, 20, 378, 240, 79, 126, 204, 101, 64, 46, 27, 54, 67, 194, 303, 84, 176, 96, 81, 49, 32, 25, 85, 126, 298, 221, 316, 176, 220, 168, 88, 69, 27, 42, 109, 113, 176, 64, 221, 118, 83, 34, 29, 55, 83, 118, 204, 95, 66, 116, 270, 74, 41, 49, 78, 76, 154, 96, 77, 33, 78, 110, 23, 46, 49, 54, 115, 101, 79, 29, 23, 52, 15, 22, 53, 35, 86, 65, 81, 52, 26, 12, 12, 26, 52, 81, 65, 86, 35, 53, 22, 15, 52, 23, 29, 79, 101, 115, 54, 49, 46, 23, 110, 78, 33, 77, 96, 154, 76, 78, 49, 41, 74, 270, 116, 66, 95, 204, 118, 83, 55, 29, 34, 83, 118, 221, 64, 176, 113, 109, 42, 27, 69, 88, 168, 220, 176, 316, 221, 298, 126, 85, 25, 32, 49, 81, 96, 176, 84, 303, 194, 67, 54, 27, 46, 64, 101, 204, 126, 79, 240, 378, 20, 32, 32, 61, 65, 154, 113, 65, 64, 170, 15, 16, 35, 45, 52, 115, 118, 91, 47, 23, 16, 35, 34, 56, 69, 52, 71, 47, 41, 24, 33, 25, 21, 52, 77, 65, 84, 75, 60, 32, 138, 47, 30, 44, 60, 101, 118, 74, 66, 51, 110, 194, 65, 59, 76, 96, 168, 69, 54, 35, 47, 98, 298, 126, 69, 95, 220, 108, 52, 38, 27, 42, 109, 113, 176, 64, 221, 118, 83, 34, 75, 89, 128, 168, 204, 176, 357, 303, 270, 110, 60, 26, 32, 48, 77, 96, 221, 94, 558, 110, 32, 39, 33, 53, 69, 101, 220, 116, 92, 382, 14, 14, 27, 35, 58, 65, 168, 118, 78, 26, 276, 72, 28, 22, 51, 58, 36, 224, 315, 310, 37, 35, 23, 23, 51, 69, 54, 125, 193, 161, 55, 26, 23, 27, 34, 77, 76, 134, 98, 155, 74, 96, 29, 34, 40, 60, 118, 128, 69, 48, 50, 126, 118, 77, 50, 76, 113, 168, 57, 43, 38, 52, 108, 220, 95, 69, 126, 298, 98, 47, 25, 32, 49, 81, 96, 176, 84, 303, 194, 68, 382, 145, 134, 118, 154, 204, 222, 749, 558, 379, 374, 62, 25, 35, 51, 77, 113, 304, 327, 867, 584, 63, 43, 28, 51, 69, 118, 298, 241, 239, 924, 229, 20, 15, 36, 51, 45, 47, 754, 1143, 79, 172, 28, 13, 24, 51, 61, 76, 346, 489, 53, 35, 24, 22, 21, 51, 64, 74, 145, 136, 67, 80, 21, 30, 23, 34, 81, 69, 89, 41, 85, 98, 91, 44, 41, 40, 79, 108, 88, 44, 43, 57, 168, 113, 76, 50, 77, 118, 126, 50, 29, 55, 83, 118, 204, 95, 66, 116, 270, 74, 60, 26, 32, 48, 77, 96, 221, 94, 558, 110, 927, 347, 124, 84, 115, 154, 220, 303, 1044, 866, 1461, 316, 30, 19, 33, 51, 81, 118, 558, 626, 3567, 2257, 88, 6, 20, 33, 35, 54, 314, 2022, 371, 1006, 134, 9, 9, 36, 53, 50, 192, 927, 57, 72, 84, 14, 11, 24, 48, 78, 98, 382, 46, 41, 23, 18, 9, 21, 56, 83, 69, 75, 50, 83, 52, 27, 21, 23, 52, 109, 57, 69, 44, 88, 108, 79, 40, 41, 44, 91, 98, 85, 35, 54, 69, 168, 96, 76, 59, 65, 194, 110, 54, 27, 46, 64, 101, 204, 126, 79, 240, 379, 374, 62, 25, 35, 51, 77, 113, 304, 327, 866, 2022, 754, 224, 71, 86, 115, 168, 298, 558, 1474 };
int brr[1000];
int magic(int x)
{
    if (x < 10)
        return x;
    int cnt = 0;
    while (x) {
        brr[++cnt] = x % 10;
        x /= 10;
    }
    int ans = 0;
    for (int i = cnt; i > 1; i--) {
        ans = ans * 10 + abs(brr[i] - brr[i - 1]);
    }
    return magic(ans);
}

int solve(int n)
{
    int ans = arr[n / 1000000];
    for (int i = n / 1000000 * 1000000 + 1; i <= n; i++) {
        if (magic(i) == 7)
            ans++;
    }
    return ans;
}

int main()
{
    for (int i = 1; i <= 1000; i++)
        arr[i] += arr[i - 1];
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d\n", solve(b) - solve(a - 1));
    return 0;
}
posted @ 2020-12-05 17:10  Valk3  阅读(138)  评论(0编辑  收藏  举报