YAPTCHA

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

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
翻译:求Sn。中括号([])表示取整。
解题过程:
令p=3*k+7。
威尔逊定理:当且仅当p为素数,(p-1)! ≡ -1 (mod p) → (p-1)!+1 ≡ 0 (mod p)
1.若p是素数, ( (p-1)!+1 )/p是个整数,设为x,则(p-1)!/p比这个整数小一点点,取整后是x-1,
则[  ( (p-1)!+1 )/p - [(p-1)!/p] ]=1;
2.若p是合数,(p-1)!/p是个整数,设为y,则( (p-1)!+1 )/p比这个整数大一点点,取整后还是y,
则[  ( (p-1)!+1 )/p - [(p-1)!/p] ]=0;
3.前缀和打表Sn。
 #include <iostream>
#include<stdio.h>
#include <algorithm>
#include<string.h>
#include<cstring>
#include<math.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxx=;
int prime[];
bool vis[];
int ans[];
int cnt; void init()
{
cnt=;
memset(vis,true,sizeof(vis));
vis[]=vis[]=false;
for(int i=;i<=maxx;i++)//欧拉筛
{
if(vis[i])
prime[cnt++]=i;
for(int j=;j<cnt && prime[j]*i<=maxx;j++)
{
vis[ prime[j]*i ]=false;
if( i%prime[j]== ) break;
}
}
memset(ans,,sizeof(ans));
int p;
for(int k=;k<=;k++)
{
p=*k+;
if(vis[p])
ans[k]=ans[k-]+;
else
ans[k]=ans[k-];
}
} int main()///hdu2973,威尔逊定理+前缀和
{
init();
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",ans[n]);
}
return ;
}

hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)的更多相关文章

  1. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

  2. 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛

    欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...

  3. [51NOD1181]质数中的质数(质数筛法)(欧拉筛)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...

  4. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

  5. 欧拉筛,线性筛,洛谷P2158仪仗队

    题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...

  6. pku-2909 (欧拉筛)

    题意:哥德巴赫猜想.问一个大于2的偶数能被几对素数对相加. 思路:欧拉筛,因为在n<215,在3万多,一个欧拉筛得时间差不多4*104, 那么筛出来的素数有4千多个,那么两两组合直接打表,时间复 ...

  7. hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)

    Deciphering Password Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. POJ-3126.PrimePath(欧拉筛素数打表 + BFS)

    给出一篇有关素数线性筛和区间筛的博客,有兴趣的读者可以自取. 本题大意: 给定两个四位的素数,没有前导零,每次变换其中的一位,最终使得两个素数相等,输出最小变换次数.要求变换过程中的数也都是素数. 本 ...

  9. 欧拉筛(线性筛) & 洛谷 P3383 【模板】线性筛素数

    嗯.... 埃氏筛和欧拉筛的思想都是相似的: 如果一个数是素数,那么它的所有倍数都不是素数.... 这里主要介绍一下欧拉筛的思路:(欧拉筛的复杂度大约在O(n)左右... 定义一个prime数组,这个 ...

随机推荐

  1. 9 个鲜为人知的 Python 数据科学库

    除了 pandas.scikit-learn 和 matplotlib,还要学习一些用 Python 进行数据科学的新技巧. Python 是一种令人惊叹的语言.事实上,它是世界上增长最快的编程语言之 ...

  2. 02-spark sql

    1.概念 Spark SQL是一个用来处理结构化数据的Spark组件. 优点:  ①SparkSQL是一个SQL解析引擎,将SQL解析成特殊的RDD(DataFrame),然后在Spark集群中运行 ...

  3. 安全测试8_Web安全实战1(DVWA部署)

    1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...

  4. vim basic

    1.基本用法 1.1.编辑模式 1.2.底行模式 1.3.环境配置 1.4.使用时发现的 2.编辑多个文档 3.选项 1.基本用法 1.1.编辑模式 插入 插入命令 插入位置 i 光标左侧 a 光标右 ...

  5. python2.7与3.5版本中:编码格式及编码转换

    主要说明编码之间的转换方法 2.7版本: 1 # -*- coding:utf-8 -*- 2 a = "迪丽热巴" 3 a_unicode = a.decode("ut ...

  6. 1. 在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

    在config.ini文件中加入dm.park.time=1,会使uap中的tomcat启动加快

  7. leetcode1014

    这道题暴力算法,会超时: class Solution(object): def maxScoreSightseeingPair(self, A: 'List[int]') -> int: n ...

  8. day30-模块和包

    一.模块介绍 1.什么是模块 在python中,一个函数封装一个功能,当一个文件中包含很多个函数,而我们在其他程序中经常会用到这个文件中的功能时,那么我们就可以将这个包含多个函数的文件封装成一个模块, ...

  9. WDA-Web Dynpro的POWL(个人对象工作清单)

    POWL(Personal Object Worklist) for Web Dynpro 转载地址:https://blogs.sap.com/2013/02/15/powlpersonal-obj ...

  10. jquery_ajax 地址三级联动

    jquery 的三级地址联动,原理与javascript类似,只是在触发请求的时候,使用封装好的 $.get ,$post,$.ajax 方法去执行,其余的都是一样的,后台服务器请求文件是一样的,前台 ...