先上题目:

YAPTCHA

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

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
 
  题意很简单,t个case,每个case给你一个n,根据公式求出结果然后直接输出结果。
  先说一下这一题需要用到什么,一是筛素数,二是威尔逊定理。
  威尔逊定理如下:
          p是素数,则 (p-1)! ≡ -1 (mod p)
  为什么需要用到威尔逊定理,通过观察题目给出的公式可以令p=3k+7,则通过分析,当p为素数的时候Sn中第k个数就是1,否则p不是素数的时候第k个数就是0。所以才需要筛素数,然后再判断给出的范围里面每一个Sn,然后每一次查询就直接输出答案。
  在ac之前wa了两次,后来发现是数组开小了= =了,所以这里开大100其实没有关系。
  代码上两份,一份是完全自己写的,筛素数的时间复杂度为nloglogn,提交以后返回的时间是800+ms,另一份是用上人家的输入输出挂,速度会去到200+ms,染过吧筛素数的方法换成线性筛法的话还会快大概30ms。
 
上代码:
#include <stdio.h>
#include <string.h>
#define MAX 1000110
using namespace std; bool pri[MAX*];
int ans[MAX]; void dedeal()
{
long long i,j,n;
n=(MAX-)*;
memset(pri,,sizeof(pri));
pri[]=pri[]=;
for(i=;i<=n;i++)
{
if(!pri[i])
for(j=i*i;j<=n;j+=i) pri[j]=;
}
} void deal()
{
int i,n;
n=(MAX-);
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
if(!pri[*i+])
ans[i]=ans[i-]+;
else ans[i]=ans[i-];
}
} int main()
{
int n,t;
//freopen("data.txt","r",stdin);
dedeal();
deal();
scanf("%d",&t);
while(t--)
{
scanf("%d\n",&n);
printf("%d\n",ans[n]);
}
return ;
}

2973

提速版

 #include <iostream>
#include <string.h>
#include <stdio.h>
#define MAX 1000110
using namespace std ;
bool pri[] ;
int ans[] ; void dedeal()
{
long long i,j,n;
n=(MAX-)*;
memset(pri,,sizeof(pri));
pri[]=pri[]=;
for(i=;i<=n;i++)
{
if(!pri[i])
for(j=i*i;j<=n;j+=i) pri[j]=;
}
} void deal()
{
int i,n;
n=(MAX-);
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
if(!pri[*i+])
ans[i]=ans[i-]+;
else ans[i]=ans[i-];
}
} inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} void print_f(int x){
if(x==)return;
print_f(x/);
putchar(x%+'');
}
int main()
{
//freopen("data.txt","r",stdin);
int t ;
dedeal();
deal();
scan_d(t) ;
while(t--)
{
int n ;
scan_d(n) ;
if(n==)
putchar('') ;
else
print_f(ans[n]) ;
putchar('\n') ;
}
return ;
}

2973

HDU - 2973 - YAPTCHA的更多相关文章

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

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

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

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

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

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

  4. hdoj 2111 Saving HDU

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 邪恶的C++

    曾经看到一篇很有趣的文章,今天转载一下.抱歉的是没有找到最原始的版本,算是遗憾吧. ---------- 华丽的分割线 ---------- Linus曾经(2007年9月)在新闻组gmane.com ...

  2. 3n+1问题

    #include <stdio.h> #include <math.h> // 算法竞赛的目标是编程对任意输入均得到正确的结果. // 请先独立完成,如果有困难可以翻阅本书代码 ...

  3. jquerymobile之collapsible可折叠块标题内容动态显示

    jquery mobile提供了一种可折叠的组件--data-role="collapsible",这种组件可以通过点击折叠块头部来展开/折叠块内的内容,详细组件说明可参考w3cs ...

  4. System.IO.Path 操作

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  5. [BZOJ 1741] Asteroids

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1741 [算法] 将每颗小行星的行,列相连,问题就转化为了求这张图的最小覆盖 由kon ...

  6. B1001 狼抓兔子 最小割

    题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  7. Java中继承,类的高级概念的知识点

    1. 继承含义 在面向对象编程中,可以通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类,这种方式称为继承(inheritance). 2. 继承的优点 A.代码的可重用性 B.子类可以扩 ...

  8. .NET Core Run On Docker By Kubernetes 系列文章汇总

    前言介绍 .NET Core是微软新一代主力编程平台,开源.免费.跨平台.轻量级.高性能,支持Linux.Docker.k8s等环境,适合开发微服务.云原生.大型互联网应用.全开源解决方案. Dock ...

  9. python 46 盒模型 与盒模型布局

    一:盒模型 1.  盒模型的概念 广义盒模型:文档中所有功能性及内容性标签,及文档中显示性标签 侠义盒模型:文档中以块级形式存在的标签(块级标签拥有盒模型100%特性且最常用) 盒模型组成:margi ...

  10. python大杂铺

      python中continue,break,return三者之间的区别 return 会直接令函数返回,所有该函数体内的代码都不再执行了,所以该函数体内的循环也不可能再继续运行. break:跳出 ...