HDU 5391 Zball in Tina Town威尔逊定理

Posted 00isok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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






以上是关于HDU 5391 Zball in Tina Town威尔逊定理的主要内容,如果未能解决你的问题,请参考以下文章

hdu5391Zball in Tina Town

HDU5391Zball in Tina Town

BC - Zball in Tina Town (质数 + 找规律)

HDU5392 Infoplane in Tina TownLCM

HDU.5394.Trie in Tina Town(回文树)

HDU多校第四场1005 Didn‘t I Say to Make My Abilities Average in the Next Life?! 单调栈+莫队