先上题目:

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. javase - 点餐系统

    public class OrderMsg { public static void main(String[] args) throws Exception { /** * 订餐人姓名.选择菜品.送 ...

  2. springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?

    2018-07-31 11:56:18.812 WARN 10316 --- [ main] ConfigServletWebServerApplicationContext : Exception ...

  3. 一个简单的JS日期挂历脚本

    分享一个JS脚本做的日期挂历,在需要的时候可以引入你的程序. 如需单独引入这个脚本,请将它保存在一个文件中然后引入它:如这样 <script type="text/javascript ...

  4. 把一个文件夹下的多个csv文件合并到一个excel的多个sheet

    #!/usr/bin/env python3 # -*- coding: UTF-8 -*- import pandas as pd import os import re if __name__ = ...

  5. HDU3533 Escape

    题目: The students of the HEU are maneuvering for their military training. The red army and the blue a ...

  6. input点击修改样式

    <input id="geren" type="button" value="个人奖励" style="BORDER-TOP ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  8. asp.net MVC 路由注册

    1.命名空间的优先级 在路由注册时指定的命名空间比当前 ControllerBuilder 的默认命名空间具有更高的匹配优先级,但是对于这两个集合中的所有命名空间却具有相同的匹配优先级.换句话说,用于 ...

  9. 【SQL】通过rowid查找及删除重复记录

    新建T表如下: SQL> select * from t; X Y ---------- --          1 a          1 a          1 a          2 ...

  10. 时序分析:串匹配-KMP算法

    图像处理与模式识别的教科书使用大量的章节来描述空域的模式识别方法.从图像底层特征提取.贝叶斯方法到多层神经网络方法,一般不讨论到对象随时间变化的情况,视频处理应用和在线学习方法使研究对象开始向时域延伸 ...