首页 > ACM题库 > HDU-杭电 > HDU 2973-YAPTCHA-数论-[解题报告]HOJ
2014
02-24

HDU 2973-YAPTCHA-数论-[解题报告]HOJ

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.

输入:

The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).

输出:

The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).

样例输入:

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

样例输出:

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

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2973

 

题意:给你一个n,根据公式求出结果然后直接输出结果。

题解:这里要用到一个数论定理——威尔逊定理

当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p )
也可以写作:若p为质数,则p可整除(p-1)!+1
对于此题,判断3i+7是否为质数即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)

typedef long long LL;
const int N=3100100;
const int mod=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;

int pr[N];
int s[N];

void prime()
{
    mset(pr,0);
    forb(i,2,N)
    {
        if(pr[i]==0)
            for(int j=i+i;j<N;j+=i)
                pr[j]=1;
    }
}

int sn(int k)
{
    int t=3*k+7;
    if(pr[t]==0)//素数
        return 1;
    else
        return 0;
}

void suan()
{
    s[0]=0;
    s[1]=0;
    ford(i,2,1001100)
        s[i]=(s[i-1]+sn(i));
}

int main()
{
//    freopen("input.txt","r",stdin);
    int T;
    si1(T);
    prime();
    suan();
    while(T--)
    {
        int n;
        si1(n);
        pi1(s[n]);
    }

    return 0;
}

 

解题参考:http://blog.csdn.net/xh_reventon/article/details/11827209