zoukankan      html  css  js  c++  java
  • [HDOJ5391]Zball in Tina Town

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5391

      考的数论中的威尔逊定理,题目直接给出了威尔逊定理的表达式的语言描述。

      在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于实际操作意义不大。

      本题特别注意的是n=4的时候,要特判一下,是个trick。cout的话,也是会超时的。还有,本题数据给的是2<=n<=10^9,需要考虑的是如果打表的话,复杂的是O(nsqrt(n)),如果单独判的话是O(n),所以不选择打表。代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <map>
    #include <set>
    #include <cmath>
    #include <vector>
    #include <deque>
    #include <queue>
    
    using namespace std;
    typedef long long LL;
    
    int main() {
        int T;
        scanf("%d", &T);
        while(T--) {
            int flag = 0;
            int n;
            scanf("%d", &n);
            int m = int(sqrt(n));
            for (int i = 2; i < m; i++)
            {
                if (n % i == 0)
                {
                    flag++;
                    break;
                }
            }
            if(n == 0 || n == 1) {
                printf("1
    ");
            }
            else if(n == 2) {
                printf("2
    ");
            }
            else if(!flag) {
                printf("%d
    ", n-1);
            }
            else {
                printf("0
    ");
            }
        }
    }
    View Code
  • 相关阅读:
    python 装饰器
    git
    JS原生方法实现jQuery的ready()
    js获取css属性方法
    列表页调出点击量
    数组操作
    判断IE版本
    判断IE浏览器用IE条件表达式
    [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
    复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html
  • 原文地址:https://www.cnblogs.com/kirai/p/4733400.html
Copyright © 2011-2022 走看看