参考博客

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. 使用类加载器加载配置文件/getClassLoader().getResourceAsStream()

    加载配置文件的两种方式; 1. 2. 两者相比,第一种方式更为方便,因为文件目录发生改变就会找不到.

  2. PDF Document Creation, Viewing

    [PDF Document Creation, Viewing, and Transforming] Quartz provides the data type CGPDFDocumentRef to ...

  3. gcc支持的一种结构体赋值方式

    struct info{ int a; char b; struct fd{    int c;    int d;          }fg;}; 其实我们也可以这样赋值:同样对于其他的类型也是一样 ...

  4. AngularJS-$scope类

    一.$scope的作用 用来在controller和view中进行数据.事件的传递 二.$scope和$rootscope的区别 1.$rootscope根作用域 2.$rootscope可以实现多个 ...

  5. 看图说说Sun HotSpot虚拟机对象

  6. Alpha冲刺 - (6/10)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 基于ssm框架的前后端交互测试,结合 ...

  7. Debug_with_docker_in_pycharm

    Debug with docker in pycharm Why As I really really appreciate it that we can have a isolated develo ...

  8. PostgreSQL 区域设置

    安装PostgreSQL 10.3 windows版本时区域请选择"default locale",安装成功后输入命令: show lc_ctype; show lc_collat ...

  9. c#进阶之lambda表达式

    阅读之前,先确保对委托有基本的了解,传送门 c#进阶之浅析委托和事件. lambda表达式雏形第一步 在委托那篇文章,绑定的的方法都是具名函数,为了简化书写,可以换成匿名函数 public deleg ...

  10. 域名通过infopath访问webservice出现401错误

    解决办法: 跟服务器有关,需要再每台服务器进行以下配置 New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name " ...