hdu5391 Zball in Tina town

Source: Internet
Author: User

Problem Descriptiontina 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 1 Time as large as its original size. On the second day,it'll become 2 Times as large as the size on the first day. On the n-th day,it'll become n 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.

Inputthe 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.
T ≤< Span class= "Texatom" id= "mathjax-span-24" style= "display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px ">10 5 ,2≤N≤< Span class= "Texatom" id= "mathjax-span-36" style= "display:inline; position:static; border:0px; padding:0px; margin:0px; vertical-align:0px ">10 9

Outputfor each test case, output an integer representing the answer.
Sample Input
2310

Sample Output
20

This problem is (n-1)!%n, according to the table found that when n is 4 output 2, the other if it is a prime number of the output n-1, otherwise output 0. Using the linear prime sieve method, the time is much less.

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < math.h> #include <vector> #include <map> #include <set> #include <queue> #include <stack > #include <string> #include <algorithm>using namespace std; #define MAXN 400000int prime[maxn+10],vis[    Maxn+10],tot;void init () {int i,j;    tot=0;        for (i=2;i<=maxn;i++) {if (!vis[i]) {tot++;p rime[tot]=i;            } for (J=1;j<=tot &&prime[j]*i<=maxn;j++) {vis[prime[j]*i]=1;        if (i%prime[j]==0) break;    }}}int Main () {int n,m,i,j,num,t,flag;    Init ();    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        if (n==4) {printf ("2\n"); continue;            } if (N&LT;=MAXN) {if (!vis[n]) {printf ("%d\n", n-1);            } else printf ("0\n");        Continue        } flag=1; For (I=1;i<=tot && prime[i]*prime[i]<=n;i++) {if (n%prime[i]==0) {flag=0;break;        }} if (flag) printf ("%d\n", n-1);    else printf ("0\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu5391 Zball in Tina town

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.