先上题目:

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. oc42--引用计数器

    /* main.m 堆里面的内存释放是根据引用计数器,所以就是操作引用计数器. 创建一个对象,对象里面就有一个引用计数器,有多少指针指向它. 引用计数器为0就释放.任何一个对象初始化时就是1,所以 { ...

  2. PCB Genesis加邮票孔(弧形连接位宽度校正)实现算法

    采用弧形作为加接位,当两边距离较远时,会造成连接位变窄,由于之前算法是基于连接位间距做为半径画弧, 必然存在这个缺陷,这边做少许的改进解决此问题. 现将几个种增加孤形连接位的图形对比如下: 一.两边外 ...

  3. Greenplum使用教程

    Greenplum简介 GreenPlum是一个关系型数据库集群.,它实际上是由多个独立的数据库服务组合成的逻辑数据库.GreenPlum是基于PostgreSQL(开源数据库)的分布式数据库,它采用 ...

  4. JS代码放在哪里比较好!

    在页面上加上<script></script>只有2个地方:head中,body体中 如果外部的JS文件,在head中加,写页面特效js放在body后面. <html&g ...

  5. TCP/IP详解(三)

    超时与重传: TCP在发送一个包时,启动一个定时器,如果在定时器溢出之前没有收到ACK,则认为发出的包丢失了,此时会重传丢失的包.这就是超时重传. 其中定时器的时间不是一个固定值,它是根据RTT计算的 ...

  6. ThinkPHP __PUBLIC__的定义 __ROOT__等 常用 常量的定义

    '__TMPL__' => APP_TMPL_PATH, // 项目模板目录 '__ROOT__' => __ROOT__, // 当前网站地址 '__APP__' => __APP ...

  7. 关于sizeof()、size()的有些问题

    #include<iostream>using namespace std; int main() { char a[] = "abcdefg"; string s = ...

  8. JavaScript 判断手机端操作系统(Andorid/IOS)

    androidURL = "http://xxx/xxx.apk"; var browser = { versions: function() { var u = navigato ...

  9. linux ssh 经常断开 的解决方法

    1.现象 在linux ,用ssh进行远程连接时,经常会发生长时间后断线,或者是无响应,就像卡住的感觉(键盘输入不进去). 2.解决方法 在ssh客户端的linux设置 # sudo vim /etc ...

  10. (转)基于Metronic的Bootstrap开发框架经验总结(4)--Bootstrap图标的提取和利用

    http://www.cnblogs.com/wuhuacong/p/4762924.html 在前面的一篇随笔<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模 ...