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.

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

  1. 13
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 100
  13. 1000
  14. 10000

Sample Output

  1. 0
  2. 1
  3. 1
  4. 2
  5. 2
  6. 2
  7. 2
  8. 3
  9. 3
  10. 4
  11. 28
  12. 207
  13. 1609

 题意:求这个公式的前缀和。

思路:不难发现,式子里做减法要么为1.要么为0,为1当且仅当((3k+6)!+1)%(3k+7)==0;

然后我就不知道了,百度了下原来就是威尔逊定理:p是素数的充分必要条件是,((p-1)!+1)%p==0;

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=;
  4. int p[maxn],vis[maxn],ans[maxn],cnt;
  5. void solve()
  6. {
  7. vis[]=;
  8. for(int i=;i<maxn;i++){
  9. if(!vis[i]) p[++cnt]=i;
  10. for(int j=;j<=cnt&&p[j]*i<maxn;j++){
  11. vis[i*p[j]]=;
  12. if(!(i%p[j])) break;
  13. }
  14. }
  15. }
  16. int main() {
  17. solve();
  18. int T,N; scanf("%d",&T);
  19. for(int i=;i<=;i++) ans[i]=ans[i-]+(!vis[*i+]);
  20. while(T--){
  21. scanf("%d",&N);
  22. printf("%d\n",ans[N]);
  23. }
  24. return ;
  25. }

HDU - 2973:YAPTCHA (威尔逊定理)的更多相关文章

  1. hdu 2973"YAPTCHA"(威尔逊定理)

    传送门 题意: 给出自然数 n,计算出 Sn 的值,其中 [ x ]表示不大于 x 的最大整数. 题解: 根据威尔逊定理,如果 p 为素数,那么 (p-1)! ≡ -1(mod p),即 (p-1)! ...

  2. HDU 2973 YAPTCHA (威尔逊定理)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU - 2973 - YAPTCHA

    先上题目: YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. HDU2937 YAPTCHA(威尔逊定理)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. hdu2973 YAPTCHA【威尔逊定理】

    <题目链接> 题目大意: The task that is presented to anyone visiting the start page of the math departme ...

  6. HDU 5391 Zball in Tina Town【威尔逊定理】

    <题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...

  7. YAPTCHA UVALive - 4382(换元+威尔逊定理)

    题意就是叫你求上述那个公式在不同N下的结果. 思路:很显然的将上述式子换下元另p=3k+7则有 Σ[(p-1)!+1/p-[(p-1)!/p]] 接下来用到一个威尔逊定理,如果p为素数则 ( p -1 ...

  8. HDU 6608:Fansblog(威尔逊定理)

    Fansblog Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  9. HDU2973(威尔逊定理)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 【分类】AlexNet论文总结

    目录 0. 论文链接 1. 概述 2. 对数据集的处理 3. 网络模型 3.1 ReLU Nonlinearity 3.2 Training on multiple GPUs 3.3 Local Re ...

  2. js 捕获型事件

    true 为捕获型事件 false 为冒泡型事件

  3. 常见HTTP状态(304,)

    一.1XX(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示:已经收到请求的第一部分,正在等待其余部分. 101(切换协议) 请 ...

  4. python+opencv链接摄像头

    import cv2 import numpy as numpy cap=cv2.VideoCapture(0) #设置显示分辨率和FPS ,不设置的话会非常卡 cap.set(cv2.CAP_PRO ...

  5. stdclass

    $item = new stdClass();      $item->goods_id = $item_goods->getID();      $item->goods_name ...

  6. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  7. 转载:WEB前端的优化

    一.HTML优化 避免使用Iframe Iframe也叫内联frame,可以把一个HTML文档嵌入到另一个文档中.使用iframe的好处是被嵌入的文档可以完全独立于其父文档,凭借此特点我们通常可以使浏 ...

  8. angular $q的学习笔记转帖

    http://blog.segmentfault.com/bornkiller/1190000000402555 angular $q的一个不错的学习笔记

  9. Android Studio 3.0 及个版本下载和 gradle 各版本下载

    Android Studio 3.0 下载地址: 链接:http://pan.baidu.com/s/1jHVuOQi 密码:3pd0 Android Studio 3.0 包含了三大主要功能: 一套 ...

  10. SpringMvc+mybatis mybatis在xml文件中大于小于号处理

    方法一:转移字符 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE = AND start_date <= CURRENT_DATE ...