参考博客

HDU-2973

题目

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



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为素数时:

\[(p-1)!\equiv -1(mod\ p)
\]

否则

\[(p-1)!\equiv 0(mod\ p)
\]

\[a_n=[\frac {(3k+6)!+1}{3k+7}-[\frac {(3k+6)!}{3k+7}]]
\]

所以当(3k+7)为素数时,a_n为1,否则为0

\[[\frac {(3k+6)!+1}{3k+7}-[\frac {(3k+6)!}{3k+7}]]=[\frac {kp+p-1+1}{p}-[\frac {kp+p-1}{p}]]=[k+1-k]=1
\]

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm> using namespace std; typedef long long LL;
const int maxn=1e6+5;
const double pi = acos(-1);
const int mod=1e9+7;
const int N = 1000005; int vis[N * 4], p[N * 4];
int ans[N];
void Init()\\埃式筛
{
for(int i = 2;(i - 7) / 3 < N;++i)
{
if(!vis[i])
{
if((i - 7) % 3 == 0)
p[(i - 7) / 3] = 1;
for(int j = i + i;j <= N * 4;j += i)
vis[j] = true;
}
}
for(int i = 1;i < N;++i)
ans[i] = ans[i - 1] + p[i];
} int main()
{
Init();
int T;
cin >> T;
while(T--)
{
int n;
cin >> n;
cout << ans[n] << endl;
}
return 0;
}

[素数定理](https://en.wikipedia.org/wiki/Dirichlet's_theorem_on_arithmetic_progressions)

当a、b为素数时,则形如 a+nb 的素数有无穷多个

威尔逊定理--HDU2973的更多相关文章

  1. HDU2973(威尔逊定理)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. hdu2973 YAPTCHA【威尔逊定理】

    <题目链接> 题目大意: The task that is presented to anyone visiting the start page of the math departme ...

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

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. hdu5391 Zball in Tina Town(威尔逊定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Zball in Tina Town Time Limit: 3000/1500 ...

  5. hdu 2973"YAPTCHA"(威尔逊定理)

    传送门 题意: 给出自然数 n,计算出 Sn 的值,其中 [ x ]表示不大于 x 的最大整数. 题解: 根据威尔逊定理,如果 p 为素数,那么 (p-1)! ≡ -1(mod p),即 (p-1)! ...

  6. HDU 5391 Zball in Tina Town【威尔逊定理】

    <题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...

  7. YAPTCHA UVALive - 4382(换元+威尔逊定理)

    题意就是叫你求上述那个公式在不同N下的结果. 思路:很显然的将上述式子换下元另p=3k+7则有 Σ[(p-1)!+1/p-[(p-1)!/p]] 接下来用到一个威尔逊定理,如果p为素数则 ( p -1 ...

  8. HDU - 2973:YAPTCHA (威尔逊定理)

    The math department has been having problems lately. Due to immense amount of unsolicited automated ...

  9. HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)

    Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...

随机推荐

  1. va泛型

    va泛型 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的 ...

  2. ramfs和tmpfs的区别

    简介 ramfs和tmpfs是在内存上建立的文件系统(Filesystem).其优点是读写速度很快,但存在掉电丢失的风险.如果一个进程的性能瓶颈是硬盘的读写,那么可以考虑在ramfs或tmpfs上进行 ...

  3. visual studio 2013 git 记住密码

    原有配置: C:\Users\Administrator 下.gitconfig内容为 [user] name = lijf4 email = lijf4@lenovo.com 删除,修改为 [cre ...

  4. Ubuntu 14.04 install emacs 24.5

    1.前期准备工作 2.安装基础构件工具 3.下载emacs编译需要的依赖库 4.下载emacs24.5编译安装 5.下载并安装我的emacs配置文件 6.配置tmux和zsh 1. 前期准备工作 在阿 ...

  5. SQL描述(2)

    很久之前就想写出来,就是因为自己太懒,憋了怎么久.本文关于使用ORACLE分析函数对一些经济指标进行计算.表indi_value有3个关键的字段:indi_date,indi_value,indi_i ...

  6. [label][转载][paypal]paypal在线支付接口的WEB语言设置

    http://stephen830.iteye.com/blog/274072 ★★★ 本篇为原创,需要引用转载的朋友请注明:< http://stephen830.iteye.com/blog ...

  7. EBS报错FRM-92095:Oracle JInitiator版本太旧,请安装版本1.1.8.2或更高版本

    EBS打开报错FRM-92095:Oracle JInitiator版本太旧,请安装版本1.1.8.2或更高版本 打开EBS的form,系统报错,中文提示信息是:FRM-92095:oracle ji ...

  8. ORACLE PATCH 版本的查询 PL/SQL

    --ORACLE PATCH 版本的查询 PL/SQL SELECT DD.PATCH_NAME,        PP.CREATION_DATE,        PP.DRIVER_FILE_NAM ...

  9. svn自动更新服务器最新代码

    1.很简单打开dos界面 cd到svn exe目录下,运行 cd C:\Program Files\TortoiseSVN\bin    --svn安装目录(作者使用时TortoiseSVN客户端,其 ...

  10. Android中Textview显示Html,图文混排,支持图片点击放大

    本文首发于网易云社区 对于呈现Html文本来说,Android提供的Webview控件可以得到很好的效果,但使用Webview控件的弊端是效率相对比较低,对于呈现简单的html文本的话,杀鸡不必使用牛 ...