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.

InputThe first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).OutputFor 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

 题意:求这个公式的前缀和。

思路:不难发现,式子里做减法要么为1.要么为0,为1当且仅当((3k+6)!+1)%(3k+7)==0;

然后我就不知道了,百度了下原来就是威尔逊定理:p是素数的充分必要条件是,((p-1)!+1)%p==0;

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int p[maxn],vis[maxn],ans[maxn],cnt;
void solve()
{
vis[]=;
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=;j<=cnt&&p[j]*i<maxn;j++){
vis[i*p[j]]=;
if(!(i%p[j])) break;
}
}
}
int main() {
solve();
int T,N; scanf("%d",&T);
for(int i=;i<=;i++) ans[i]=ans[i-]+(!vis[*i+]);
while(T--){
scanf("%d",&N);
printf("%d\n",ans[N]);
}
return ;
}

HDU - 2973:YAPTCHA (威尔逊定理)的更多相关文章

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

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

  2. HDU 2973 YAPTCHA (威尔逊定理)

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

  3. HDU - 2973 - YAPTCHA

    先上题目: YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. HDU2937 YAPTCHA(威尔逊定理)

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

  5. hdu2973 YAPTCHA【威尔逊定理】

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

  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 6608:Fansblog(威尔逊定理)

    Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  9. HDU2973(威尔逊定理)

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

随机推荐

  1. 解题报告:hdu1159 common consequence LCS裸题

    2017-09-02 17:07:42 writer:pprp 通过这个题温习了一下刚学的LCS 代码如下: /* @theme:hdu1159 @writer:pprp @begin:17:01 @ ...

  2. Python学习札记(一) 初始python

    参考: 廖雪峰教程:Python简介 笔记 1.C语言是可以用来编写操作系统的贴近硬件的语言,所以,C语言适合开发那些追求运行速度.充分发挥硬件性能的程序.而Python是用来编写应用程序的高级编程语 ...

  3. ASP.NET 中 POST 数据并跳转页面(译自 Redirect and POST in ASP.NET)

    本文翻译自 Samer Abu Rabie 的 <Redirect and POST in ASP.NET> 简介        在实际项目中,我们会遇到这样一种应用场景:我们需要与第三方 ...

  4. mvn deploy返回400错误的几种可能

    user credentials are wrong url to server is wrong user does not have access to the deployment reposi ...

  5. Exception occurred during processing request: null报错

    报错, 恶心的一笔. 报错的地方 解决方法: 没注意到...

  6. mvc 获取 HtmlHelper 表达式值

    public static MvcHtmlString Try<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, ...

  7. SpringBoot+MyBatis简单数据访问应用

    因为实习用的是MyBatis框架,所以写一篇关于SpringBoot整合MyBatis框架的总结. 一,Pom文件 <?xml version="1.0" encoding= ...

  8. 《深入理解mybatis原理2》 Mybatis初始化机制详解

    <深入理解mybatis原理> Mybatis初始化机制详解 对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程 ...

  9. Metasploit没有db_autopwn命令的解决办法

    将附件db_autopwn.rp 拷贝到/opt/framework3/msf3/plugins/PS:db_autopwn.rp 下载地址:http://dl.vmall.com/c04w8czlc ...

  10. ORM--------Hibernate、Mybatis与Spring Data的区别

    1.概念: Hibernate :Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.着力点对象 ...