先上题目:

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. hdu 6112 今夕何夕(模拟)

    今夕何夕 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. K-means (PRML) in C++

    原始数据 #include <iostream>#include <fstream>#include <sstream>#include <vector> ...

  3. bzoj4078

    二分+2-sat 枚举第一个权值,二分第二个权值,然后2-sat检查,当第一个权值已经不能形成二分图时,再往下没意义,因为没法分成两个点集.(双指针好像跑得慢) #include<bits/st ...

  4. pair类型 这次遇到了,记录下来,方便彼此xue习

    首先,这个pair类型是在头文件utility.h中. 一个piar保存两个数据成员,是用来生成特定类型的模板,当创建一个pair时,我们必须提供两个类型名,pair的数据成员将具有对应的类型,两个类 ...

  5. bzoj1231[Usaco2008 Nov]mixup2 混乱的奶牛(状压dp)

    1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1032  Solved: 588[ ...

  6. IP地址库

    吐槽 前两天一个线上的IP地址库除了点幺蛾子,一查代码,发现用的库早就不更新了,遂决定换库,有几个方案: 纯真数据库 IPIP数据库 GeoIP 纯真数据库是大码农的福音,免费,但是精度一般:IPIP ...

  7. Spring Boot (9) mybatis全注解化

    ORM对比图 框架对比 Spring JDBC Spring Data Jpa Mybatis 性能 性能最好 性能最差 居中 代码量 多 少 多 学习成本 低 高 居中 推荐指数 ❤❤❤ ❤❤❤❤❤ ...

  8. Spring Boot (7) JdbcTemplate访问数据库

    使用jdbcTemplate操作数据库 spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习 ...

  9. 自学Python十一 Python爬虫总结

    通过几天的学习与尝试逐渐对python爬虫有了一些小小的心得,我们渐渐发现他们有很多共性,总是要去获取一系列的链接,读取网页代码,获取所需内容然后重复上面的工作,当自己运用的越来越熟练之后我们就会尝试 ...

  10. ie9长度兼容

    onchange="this.value=this.value.substring(0, 10)" onkeydown="this.value=this.value.su ...