Rist-Number(筛选)

Problem J. Rist-Number

• Time Limit: 1000ms

• Memory Limit: 65536KB

Problem Description

Rist-Number is a kind of integers that should satisfy some restrictions. Define S

as the set of all Rist-Numbers, then we have

1. 1 2 S

2. 3 × S 2 S

3. 7 × S 2 S

4. 15 × S 2 S

5. 31 × S 2 S

Obviously, S is an infinite set. Given n, judge whether n 2 S is true.

Input Format

The input contains multiple test cases.

The first line of input contains an integer T(T 100), which denotes the number

of test cases.

The following T lines describe all the queries, each with an integer n(1 n

10000).

Output Format

For each test case, output True if the statement is true, otherwise output False.

Sample Input

2

1

2

Sample Output

True

False

1

 

       题意:

       给出 T(1 ~ 100),代表有 T 组数据。存在一个集合,这个集合里面的任意一个数都可以由原来的数 X 3,X 7,X 15,X 31 而得(1也在该集合中),每组数据都有一个 N(1 ~ 10000),问这个数 N 是否存在于这个集合中,是则输出 True,不是则输出 False。

 

       思路:

       类似于素数筛选。读题有欠缺,以为 6 也存在于这个集合中,2 X 3 中的 2 明显不满足条件。所以满足的是并不是只要被 3,7,15,31中的任意一个整除就行了。要判断的是这个数的因子是不是都只是 3,7,15,31。误点在这里。离线筛选好所有数据后,直接输出即可。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 10005;

bool num[MAX];

void solve () {
        for (int i = 0; i < MAX; ++i) num[i] = false;
        num[1] = true;
        for (int i = 1; i < MAX; ++i) {
                if (num[i]) {
                        for (int j = i * 3; j < MAX; j *= 3) num[j] = true;
                        for (int j = i * 7; j < MAX; j *= 7) num[j] = true;
                        for (int j = i * 15; j < MAX; j *= 15) num[j] = true;
                        for (int j = i * 31; j < MAX; j *= 31) num[j] = true;
                }
        }
}

int main () {
        int t;
        scanf("%d", &t);
        solve();
        while (t--) {
                int n;
                scanf("%d", &n);
                if (num[n]) printf("True\n");
                else printf("False\n");
        }
        return 0;
}

 

 

你可能感兴趣的:(number)