YAPTCHA(数论,威尔逊定理)

3人阅读 评论(0) 收藏 举报
分类:

YAPTCHA

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


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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2970 2966 2967 2968 2969 

数论知识点:威尔逊定理:若p为质数,则p|(p-1)!+1,所以当3*k+1为合数时,后面式子等于前面式子,则等于0,当3*k+1为质数时,式子为1,则可直接算出3*k+1为素数的数字,前缀和加起来即可。

#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

const int maxn=1e6+5;
const int mx = 3 * 1000000 + 7 + 1;
int vis[mx],a[maxn],sum[maxn];
void shai()
{
    for(int i=2;i<mx;i++)
        if(vis[i]==0)
        {
            if((i-7)%3==0)
                a[(i-7)/3]=1;
            for(int j=i*2;j<mx;j+=i) vis[j]=1;
        }
}

int main()
{
    shai();
    for(int i=2;i<maxn;i++)
        sum[i]=sum[i-1]+a[i];
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        cout<<sum[n]<<endl;
    }
    return 0;
}

查看评论

精通memcached数据库管理深度讲解

-
  • 1970年01月01日 08:00

hdu2973 YAPTCHA(威尔逊定理)

题目链接 题意:给出n,根据公式求和 思路:此处要用到一个数论定理——威尔逊定理 题解:这里要用到一个数论定理——威尔逊定理 当且仅当p为素数时:( p -1 )! ≡ -1 ( mod ...
  • Doris1104
  • Doris1104
  • 2016-01-25 21:38:53
  • 426

Hdu 2973 YAPTCHA (数论 威尔逊定理)

虽然知道是个大水题,但最近在尝试用Windows Live Writer 写cppblog,还没有学会折叠代码的正确姿势,所以就把代码贴在这边吧。 题意很简单,由威尔逊定理,就是对一个01序列进行求和...
  • whyorwhnt
  • whyorwhnt
  • 2013-12-25 23:20:26
  • 915

收集整理威尔逊定理的证明

在求解BNUOJ 1093 YAPTCHA时,用到了威尔逊定理。在这里收集整理一下证明威尔逊定理的方法。 先说从题意出发吧。这道题是给定一个式子 再给若干个n,让你求出Sn的值。(1 暴力肯...
  • sssogs
  • sssogs
  • 2013-03-06 16:02:49
  • 4927

初等数论四大定理(威尔逊定理,欧拉定理,中国剩余定理,费马小定理)

1、威尔逊定理:在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ p-1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于...
  • mengxingyuanlove
  • mengxingyuanlove
  • 2015-09-23 17:40:58
  • 809

【数论】威尔逊定理

定理: p为素数,则((p - 1)! + 1) % p == 0 证明: 对于奇质数,令a∈A={2,3,4.....p-2},则B={a,2a,3a,.....,(p-1)a}中不会有对...
  • u011217342
  • u011217342
  • 2014-07-04 21:47:29
  • 549

hdu5391威尔逊定理

威尔逊定理 在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论...
  • qq_27803491
  • qq_27803491
  • 2015-08-16 01:37:57
  • 1003

威尔逊定理与逆定理及证明

威尔逊定理:当( p -1 )! ≡ -1 ( mod p ) 时,p为素数。 证明如下 充分性: 当p不是素数,那么令p=a*b ,其中1     (1)若a≠b,         因为...
  • mosquito_zm
  • mosquito_zm
  • 2017-08-13 10:14:16
  • 236

三个重要的同余式——威尔逊定理、费马小定理、欧拉定理 + 求幂大法的证明

一、威尔逊定理 若p为质数,则 p|(p-1)!+1 亦:(p-1)! ≡ p-1 ≡ -1(mod p) 例题: HDU 2973 YAPTCHA (威尔逊定理及其逆定理) 解题报告见http:...
  • synapse7
  • synapse7
  • 2014-02-21 16:28:11
  • 4430

(hdu 2973 YAPTCHA) <数论—威尔逊定理>

威尔逊定理
  • SDFZspli
  • SDFZspli
  • 2017-05-22 19:05:22
  • 387
    个人资料
    持之以恒
    等级:
    访问量: 881
    积分: 640
    排名: 8万+
    文章存档
    最新评论