HDU - 2973:YAPTCHA (威尔逊定理)
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.
InputThe first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).OutputFor 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
题意:求这个公式的前缀和。
思路:不难发现,式子里做减法要么为1.要么为0,为1当且仅当((3k+6)!+1)%(3k+7)==0;
然后我就不知道了,百度了下原来就是威尔逊定理:p是素数的充分必要条件是,((p-1)!+1)%p==0;
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int p[maxn],vis[maxn],ans[maxn],cnt;
void solve()
{
vis[]=;
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=;j<=cnt&&p[j]*i<maxn;j++){
vis[i*p[j]]=;
if(!(i%p[j])) break;
}
}
}
int main() {
solve();
int T,N; scanf("%d",&T);
for(int i=;i<=;i++) ans[i]=ans[i-]+(!vis[*i+]);
while(T--){
scanf("%d",&N);
printf("%d\n",ans[N]);
}
return ;
}
HDU - 2973:YAPTCHA (威尔逊定理)的更多相关文章
- hdu 2973"YAPTCHA"(威尔逊定理)
传送门 题意: 给出自然数 n,计算出 Sn 的值,其中 [ x ]表示不大于 x 的最大整数. 题解: 根据威尔逊定理,如果 p 为素数,那么 (p-1)! ≡ -1(mod p),即 (p-1)! ...
- HDU 2973 YAPTCHA (威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU - 2973 - YAPTCHA
先上题目: YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU2937 YAPTCHA(威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu2973 YAPTCHA【威尔逊定理】
<题目链接> 题目大意: The task that is presented to anyone visiting the start page of the math departme ...
- HDU 5391 Zball in Tina Town【威尔逊定理】
<题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...
- YAPTCHA UVALive - 4382(换元+威尔逊定理)
题意就是叫你求上述那个公式在不同N下的结果. 思路:很显然的将上述式子换下元另p=3k+7则有 Σ[(p-1)!+1/p-[(p-1)!/p]] 接下来用到一个威尔逊定理,如果p为素数则 ( p -1 ...
- HDU 6608:Fansblog(威尔逊定理)
Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- HDU2973(威尔逊定理)
YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- LDA学习之beta分布和Dirichlet分布
---恢复内容开始--- 今天学习LDA主题模型,看到Beta分布和Dirichlet分布一脸的茫然,这俩玩意怎么来的,再网上查阅了很多资料,当做读书笔记记下来: 先来几个名词: 共轭先验: 在贝叶斯 ...
- shell统计各省的百强县
原始数据在最后 baiqiang.txt文件中 shell命令: cat baiqiang.txt | grep -P "^国|^☆" | awk -F" " ...
- python列表解析进阶
如果要获得一个(元素为整数的)列表里面的偶数,很容易想到列表解析: [i for i in nums if i%2==0] 但是如果要使列表的长度不变,让奇数用0来填充,可能你会直接写: [i for ...
- iOS中的二维数组
首先我们知道OC中是没有二维数组的,二维数组是通过一位数组的嵌套实现的,但是别忘了我们有字面量,实际上可以和C/C++类似的简洁地创建和使用二维数组.这里总结了创建二维数组的两种方法以及数组的访问方式 ...
- flask学习(九):模板渲染和参数传递
一. 如何渲染模板 1. 模板放在templates文件夹下 2. 从flask中导入render_template函数 3. 在视图函数中,使用render_template函数,渲染模板 注意:只 ...
- 十八 Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式
我们自定义一个main.py来作为启动文件 main.py #!/usr/bin/env python # -*- coding:utf8 -*- from scrapy.cmdline import ...
- python使用tkinter做界面之颜色
python使用tkinter做界面之颜色 from tkinter import *colors = '''#FFB6C1 LightPink 浅粉红#FFC0CB Pink 粉红#DC ...
- Ansible 小手册系列 七(Ad-hoc)
Ansible提供两种方式去完成任务,一是 ad-hoc 命令,一是写 Ansible playbook.前者可以解决一些简单的任务, 后者解决较复杂的任务. ad hoc——临时的,在ansible ...
- os模块、文件压缩 、匹配文件后缀名:fnmatch glob
一.os模块 os模块:是python是系统交互的模块 import os # 0平台信息的一些操作 python是夸平台的,所以内部兼容了不同的平台 1. os.name # 操作系统 nt是win ...
- 【网络编程】inet_addr、inet_ntoa、inet_aton、inet_ntop和inet_pton区分
先上一张图 1.把ip地址转化为用于网络传输的二进制数值 int inet_aton(const char *cp, struct in_addr *inp); inet_aton() 转换网络主机地 ...