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


    <题目链接>

                                  Zball in Tina Town

    Problem Description
    Tina Town is a friendly place. People there care about each other.
    Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 time as large as its original size. On the second day,it will become 2times as large as the size on the first day. On the n-th day,it will become n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day mod n.
     
    Input
    The first line of input contains an integer T, representing the number of cases.
    The following T lines, each line contains an integer n, according to the description.
    T105,2n109
     
    Output
    For each test case, output an integer representing the answer.
     
    Sample Input
    2
    3
    10
     
    Sample Output
    2
    0
     
    解题分析:
    此题一看到 (n-1)! 就要立刻想到威尔逊定理,当n为素数时,毫无疑问 (n-1)! %n=n-1 ,但是如果n不是素数,而是合数,应该怎么办呢?我们可以简单的试几个数,如果n为合数,我们发现
    (n-1)!中总能够找出一些因子,使它们想乘为n,但是要注意4是个例外,由于4比较靠前,所以(4-1)!不一定能够凑出4,除了这种情况,其他的合数毫无疑问答案都为0。
     
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    
    int juge(int x)
    {
        for(int i=2;i*i<=x;i++)
        {
            if(x%i==0)return false;
        }
        return true;
    }
    
    int main()
    {
        
        int t;cin>>t;
        while(t--)
        {
            int n;
            scanf("%d",&n);
            if(n==4)
            {
                printf("2
    ");
            } 
            else
            {
                if(juge(n))
                {
                    printf("%d
    ",n-1);
                }
                else
                printf("0
    ");
            }        
        }
        return 0;
    }
     
     
    2018-07-31
  • 相关阅读:
    SQLSERVER Truncate使用注意事项
    SQLSERVER Truncate使用注意事项
    c#事件使用示例详解
    c#事件使用示例详解
    C#反射の反射泛型
    C#反射の反射泛型
    C#反射设置属性值和获取属性值
    1336:【例31】找树根和孩子
    1336:【例31】找树根和孩子
    1037:计算2的幂
  • 原文地址:https://www.cnblogs.com/00isok/p/9398494.html
Copyright © 2020-2023  润新知