hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)

时间:2023-03-09 13:02:43
hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)

YAPTCHA

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1490    Accepted Submission(s):
811

Problem Description
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
hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)
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
翻译:求Sn。中括号([])表示取整。
解题过程:
令p=3*k+7。
威尔逊定理:当且仅当p为素数,(p-1)! ≡ -1 (mod p) → (p-1)!+1 ≡ 0 (mod p)
1.若p是素数, ( (p-1)!+1 )/p是个整数,设为x,则(p-1)!/p比这个整数小一点点,取整后是x-1,
则[  ( (p-1)!+1 )/p - [(p-1)!/p] ]=1;
2.若p是合数,(p-1)!/p是个整数,设为y,则( (p-1)!+1 )/p比这个整数大一点点,取整后还是y,
则[  ( (p-1)!+1 )/p - [(p-1)!/p] ]=0;
3.前缀和打表Sn。
 #include <iostream>
#include<stdio.h>
#include <algorithm>
#include<string.h>
#include<cstring>
#include<math.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxx=;
int prime[];
bool vis[];
int ans[];
int cnt; void init()
{
cnt=;
memset(vis,true,sizeof(vis));
vis[]=vis[]=false;
for(int i=;i<=maxx;i++)//欧拉筛
{
if(vis[i])
prime[cnt++]=i;
for(int j=;j<cnt && prime[j]*i<=maxx;j++)
{
vis[ prime[j]*i ]=false;
if( i%prime[j]== ) break;
}
}
memset(ans,,sizeof(ans));
int p;
for(int k=;k<=;k++)
{
p=*k+;
if(vis[p])
ans[k]=ans[k-]+;
else
ans[k]=ans[k-];
}
} int main()///hdu2973,威尔逊定理+前缀和
{
init();
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",ans[n]);
}
return ;
}