XTU Happy Number (预处理)

Happy Number

Accepted : 156   Submit : 371
Time Limit : 1000 MS   Memory Limit : 65536 KB

Problem Description

Recently, Mr. Xie learn the concept of happy number. A happy number is a number contain all digit 7 or only 1 digit other than 7. For example, 777 is a happy number because 777 contail all digit 7, 7177 and 87777 both happy number because only 1 digit other
than 7. Whereas 887,799 9807,12345, all of them are not happy number. Now Mr. xie want to know for a given integer n, how many number among [1,n] are happy numbers, but counting them one by one is slow, can you help him?

Input

First line an integer t indicate there are t testcases(1≤t≤100). Then t lines follow, each line an integer n(1≤n≤106, n don‘t have leading zero).

Output

Output case number first, then the answer.

Sample Input

5
1
7
17
20
30

Sample Output

Case 1: 1
Case 2: 7
Case 3: 10
Case 4: 10
Case 5: 11

Source

daizhenyang

解析:直接打表即可。查找时用二分查找。

AC代码:

#include <bits/stdc++.h>
using namespace std;

vector<int> p;
void init(){
    for(int i=1; i<1000000; i++){
        int ans = 0, j = i;
        while(j){
            if(j % 10 != 7) ans ++;
            j /= 10;
        }
        if(ans <= 1) p.push_back(i);
    }
}

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    init();
    int t, n;
    scanf("%d", &t);
    for(int i=1; i<=t; i++){
        scanf("%d", &n);
        printf("Case %d: %d\n", i, upper_bound(p.begin(), p.end(), n) - p.begin());     //二分查找
    }
    return 0;
}
时间: 2024-11-05 20:29:02

XTU Happy Number (预处理)的相关文章

nyOJ基础题:蛇形填数

一遍AC #include <stdio.h> /* 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 输入 直接输入方陈的维数,即n的值.(n<=100) 输出 输出结果是蛇形方陈. 样例输入 3 样例输出 7 8 1 6 9 2 5 4 3 */ int main(){ int n; while(~scanf("%d", &n)){ int a[

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

XTU 1242 Yada Number 容斥

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

xtu 1242 Yada Number 打表

Yada Number       Time Limit : 2000 MS   Memory Limit : 65536 KB Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,

xtu 1242 Yada Number 容斥原理

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

XTU OJ 1210 Happy Number (暴力+打表)

Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number contain all digit 7 or only 1 digit other than 7. For example, 777 is a happy number because 777 contail all digit 7, 7177 and 87777 both happy number

BZOJ 1114 Number theory(莫比乌斯反演+预处理)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738 题意:给你一个整数序列a1, a2, a3, ... , an.求gcd(ai, aj) = 1 且 i < j的对数. 思路:利用莫比乌斯反演很快就能得到公式,但是求解时我们要知道序列中1, 2, 3, ... , max(a1, a2, ... , an)的倍数各是多少.我们用num[i]=k,来表示序列中有k个数是i的倍数,那么这部分对结果的影响是m

number(NOIP模拟赛Round 3)

好吧,神奇的题目.. 原题传送门 这道题目,神奇的字符单调栈.. 首先预处理出1~n个数(大家都会.) 然后塞进字符串里,输出答案(水~) 然后.. 我们需要将所有的字符存入单调栈中,然后乱搞,就可以输出啦. 不过注意:有的题目可能在单调栈中没有把所有的字符删完,即后面的所有字符都一样,那么此时我们需要直接把后面的多出来的位数减去即可. 还有,单调栈中如果已经减了m位之后就不能再减了 在维护单调队列时我们要尽量让排名靠前的数字被删除(贪心) 这样得到的答案才最优 (这道题我一开始做的时候没有维护

关于C++代码中的#pragma预处理指令

预处理指令是指在编译器编译代码时,提供按条件跳过源文件中的代码段(节).报告错误(错误信息以及行号)和警告条件,以及描绘源代码的不同区域的能力. 总是占用源代码中的单独一行,并且总是以 # 字符和预处理指令名称开头.# 字符的前面以及 # 字符与指令名称之间可以出现空白符. 下面是可用的预处理指令: #define 和 #undef,分别用于定义和取消定义条件编译符号. #if.#elif.#else 和 #endif,用于按条件跳过源代码中的节. #line,用于控制行号(在发布错误和警告信息