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
20This 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<=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