zoukankan      html  css  js  c++  java
  • JZ初中OJ 1566. [GDKOI]幸运锁

    题目描述

    有一把幸运锁,打开它将会给你带来好运,但开锁时需要输入一个正整数(没有前导0)。幸运锁有一种运算,对于一个正整数,返回他的相邻两位数字间的差,如1135,运算结果为22(会去掉前导0)。

    现在已知只有经过反复运算最终结果为7的数才能打开这把锁,给你一个区间[a,b],问该区间中有多少个能打开幸运锁的幸运数。
     

    输入

       第一行两个整数a,b。

    输出

       一个整数K,表示共有多少个这样的数。
     

    样例输入

    1 10

    样例输出

    1
     

    数据范围限制

    【限制】

    1<=a<=b<=10^9。

    30%的数据有b<=10^6。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int readint() {
     4     int x = 0;
     5     char c = getchar();
     6     for (; c <  '0' || c >  '9'; c = getchar());
     7     for (; c >= '0' && c <= '9'; c = getchar()) x = (x * 10) + (c - '0');
     8     return x;
     9 }
    10 long long toVal(string s) {
    11     long long sum = 0;
    12     for (int i = 0; s[i]; ++i)
    13         sum = (sum * 10) + (s[i] - '0');
    14     return sum;
    15 }
    16 const int MAXN = 170000 + 5;
    17 int x, y, hd, tl, len, ans;
    18 string s, d[MAXN];
    19 void dfs(int pos)
    20 {
    21     long long tmp = 0;
    22     if (pos == len)
    23     {
    24         tmp = toVal(s);
    25         int addnum = 0;
    26         for (; tmp <= y;)
    27         {
    28             d[++tl] = s;
    29             if (tmp >= x && tmp <= y) ++ans;
    30             
    31             s = s[0] + s;
    32             ++addnum;
    33             tmp = toVal(s);
    34         }
    35         s.erase(0, addnum);
    36         return;
    37     }
    38     tmp = (s[pos] - '0') + (d[hd][pos] - '0');
    39     if (tmp <= 9)
    40     {
    41         s += (char)(tmp + '0');
    42         dfs(pos + 1);
    43         s.erase(pos + 1, 1);
    44     }
    45     tmp = s[pos] - d[hd][pos];
    46     if (tmp >= 0 && d[hd][pos] != '0')
    47     {
    48         s += (char)(tmp + '0');
    49         dfs(pos + 1);
    50         s.erase(pos + 1, 1);
    51     }
    52 }
    53 int main()
    54 {
    55     freopen("lucky.in", "r", stdin);
    56     freopen("lucky.out", "w", stdout);
    57     x = readint();
    58     y = readint();
    59     d[tl = 1] = "7";
    60     if (x <= 7 && y >= 7) ++ans;
    61     for (; hd < tl;)
    62     {
    63         len = d[++hd].size();
    64         for (char c = '1'; c <= '9'; ++c) {
    65             s = c;
    66             dfs(0);
    67         }
    68       }
    69     printf("%d
    ", ans);
    70     fclose(stdin);
    71     fclose(stdout);
    72     return 0;
    73 }
  • 相关阅读:
    背包问题
    计蒜客lev3
    线段树BIT操作总结
    图论题收集
    Codeforces Round #607 (Div. 2) 训练总结及A-F题解
    2-sat 学习笔记
    洛谷 P3338 【ZJOI2014】力/BZOJ 3527 力 题解
    $noi.ac$ #51 array 题解
    洛谷 P3292 【SCOI2016】幸运数字/BZOJ 4568 幸运数字 题解
    洛谷 P5283 【十二省联考2019】异或粽子 题解
  • 原文地址:https://www.cnblogs.com/dsanying/p/11317211.html
Copyright © 2011-2022 走看看