HDU 5391 Zball in Tina town (prime)

Source: Internet
Author: User
Tags time limit
Zball in Tina townaccepts:397 submissions:2463 time limit:3000/1500 MS (java/others) Memory limit:262144/262144 K (java/others) P Roblem Description

Tina town was a friendly place. People there care on each of the other.

Tina has a ball called Zball. Zball is magic. It grows larger every day. On the first day, it becomes one time as large as its original size. On the second day,it would become 22times as large as the size on the first day. On the n-th day,it would become nn times as large as the size on the (n-1)-th Day. Tina want to know it size on the (n-1)-th day modulo n. Input

The first line of input contains an integer TT, representing the number of cases.

The following TT lines, each of the line contains a integer nn, according to the description. T \leq {10}^{5},2 \leq n \leq {10}^{9}t≤10 5, 2≤n≤10 9 Output

For each test case, the output of an integer representing the answer. Sample Input

2
3
10
Sample Output
2
0
Test instructions
Tina town is a kind and friendly place where everyone cares about each other.
Tina has a ball whose name is Zball. Zball is amazing, it gets bigger every day. On the first day, it will be 11 times times bigger. On the second day, it will be 22 times times bigger. On the first NN day, it will become larger than the NN.
the original volume of Zball is 11. Tina would like to know how big the volume of Zball in the first n-1n−1 of the NN.
Tina is a stupid girl, of course not, so she asked you to help her figure out the answer.
      Idea: Because (n-1) in addition to 4 of any non-prime number can be combined with less than his combination of multiplication, so all 0, if it is a prime number of words because there is no less than his other numbers multiplied to get, so (n-1)%n = = n-1   , direct output n-1
If n equals 4, direct output 2
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h >
#include <string.h>
#include <stdlib.h>
#include <math.h>

using namespace STD;

int n;
int flag;

int main ()
{
    int T;
    scanf ("%d", &t);
    while (t--)
    {
        flag = 0;
        scanf ("%d", &n);
        if (n = = 4)
        {
            printf ("2\n");
            Continue;
        }
        int m = sqrt (n-1);
        for (int i=2;i<=m;i++)
        {
            if (n%i = = 0)
            {
                flag = 1;
                break;
            }
        }
        if (flag = = 0)
        {
            printf ("%d\n", n-1);
        }
        else
        {
            printf ("0\n");
        }
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.