zoukankan      html  css  js  c++  java
  • hdu2973 YAPTCHA【威尔逊定理】

    <题目链接>

    题目大意:

    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.

    给出 t 和n,t代表样例组数,根据给出的n算出上面表达式。(注意:[x]表示,不超过x的最大整数)

    解题分析:

    首先,看到这种阶乘的形式,就很容易联想到威尔逊定理,这里的3*k+7就相当于定理中的p。威尔逊定理告诉我们:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p )。

    所以,当p为素数时,(p-1)!+1 ≡ 0 (modp)。于是,不难看出,当3*k+7为素数时,An=1,不为素数时,An=0,,所以用前缀和记录一下1~n有多少个素数即可。

    #include <cstdio>
    #include <cstring>
    
    const int maxn=1e6+100;
    
    int juge(int x)
    {
        x=3*x+7;
        for(int i=2;i*i<=x;i++)
            if(x%i==0)return false;
        return true;
    }
    
    int sum[maxn];
    
    int main()
    {
        memset(sum,0,sizeof(sum));
        sum[1]=0;
        for(int i=2;i<=maxn;i++)
        {
            if(juge(i))sum[i]=sum[i-1]+1;
            else
                sum[i]=sum[i-1];
        }
        int t;scanf("%d",&t);
        while(t--)
        {
            int n;
            scanf("%d",&n);
            printf("%d
    ",sum[n]);
        }
        return 0;
    }

    2018-07-31

  • 相关阅读:
    python2.7 pip
    opencv
    运维url收集
    转 string和byte[]的转换 (C#)
    Thrift
    Oracle AWR
    并发编程
    LED相关
    java mail 使用 gmail smtp 发送邮件
    jquery动画总结
  • 原文地址:https://www.cnblogs.com/00isok/p/9398617.html
Copyright © 2011-2022 走看看