hdu2973 YAPTCHA(威尔逊定理)

hdu2973 YAPTCHA

 The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle.


However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program).

The task that is presented to anyone visiting the start page of the math department is as follows: given a natural n, compute

这里写图片描述

where [x] denotes the largest integer not greater than x. 

Input
The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).
Output
For each n given in the input output the value of Sn.
Sample Input

13
1
2
3
4
5
6
7
8
9
10
100
1000
10000

Sample Output

0
1
1
2
2
2
2
3
3
4
28
207
1609

题意:

计算题目所给的公式值

分析:

根据公式我们发现了威尔逊定理的影子

威尔逊定理:

当且仅当p是素数的时候

(p1)!1 (mod p) ( p − 1 ) ! ≡ − 1   ( m o d   p )

也就是说 p(p1)!+1 p 整 数 ( p − 1 ) ! + 1

回到这个题目我们发现

1)如果 (3k+7) ( 3 k + 7 ) 是素数

那么式子

(3k+6)!+13k+7 ( 3 k + 6 ) ! + 1 3 k + 7

的结果是一个整数假设为m

那么后面的部分式子

(3k+6)!3k+7 ⌈ ( 3 k + 6 ) ! 3 k + 7 ⌉

的值为m-1

所以此时两部分作差后的结果为1

2)当3k+7不是素数的时候

(3k+6)!+13k+7=m. ( 3 k + 6 ) ! + 1 3 k + 7 = m . ⋯

(3k+6)!3k+7=m. ( 3 k + 6 ) ! 3 k + 7 = m . ⋯

(3k+6)!3k+7=m ⌈ ( 3 k + 6 ) ! 3 k + 7 ⌉ = m


(3k+6)!+13k+7>(3k+6)!3k+7 ( 3 k + 6 ) ! + 1 3 k + 7 > ( 3 k + 6 ) ! 3 k + 7

所以

(3k+6)!+13k+7(3k+6)!3k+7=0. ( 3 k + 6 ) ! + 1 3 k + 7 − ⌈ ( 3 k + 6 ) ! 3 k + 7 ⌉ = 0. ⋯

所以

(3k+6)!+13k+7(3k+6)!3k+7=0 ⌈ ( 3 k + 6 ) ! + 1 3 k + 7 − ⌈ ( 3 k + 6 ) ! 3 k + 7 ⌉ ⌉ = 0

故此时结果为0

因此直接筛选出素数来后直接递推初始化即可

code:

#include 
#include 
#include 
using namespace std;
const int N = 3e6+110;
int isprime[N];
int s[N];
void init(){
    memset(isprime,0,sizeof(isprime));
    for(int i = 2; i < N; i++){
        if(!isprime[i]){
            for(int j = i + i; j < N; j += i){
                isprime[j] = 1;
            }
        }
    }
    s[0] = 0,s[1] = 0;
    for(int i = 2; i <= 1000000; i++){//这里初始到1e6即可,大了RE了
        int tmp = 3 * i + 7;
        s[i] = s[i-1] + (1 - isprime[tmp]);
    }
}
int main(){
    int t;
    init();
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        printf("%d\n",s[n]);
    }
    return 0;
}

你可能感兴趣的:(数论)