HDOJ 5391-Zball in Tina Town

HDOJ 5391-Zball in Tina Town Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 817 Accepted Submission(s): 471 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 2 times 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 modulo 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. T≤$$10^5$$,2≤n≤$$10^9$$ Output For each test case, output an integer representing the answer. Sample Input 2 3 10 Sample Output 2 Source BestCoder Round #51 (div.2) Recommend hujie | We have carefully selected several similar problems for you: 5395 5394 5393 5392 5390  


本题关键在于理解题意,在BestCoder中文描述中是第n天变大n倍,这样不就是n+1倍了吗,当时一直按这个思路,以为是A176787这个序列,后来看英文以及别人的解释才明白。
On the n-th day,it will become n times as large as the size on the (n-1)-th day.
所以第n天的大小就是第n-1天的n倍,也就是n!。 最终结果即为$$(n-1)!modn$$,如果n为合数,n一定能分解成n以内的某些个数相乘,所以结果为0;如果n为素数,根据威尔逊定理,有 所以结果为$$-1modn=n-1$$。 当n=2和4时,需要特殊处理。完整代码如下: [cpp] #include<iostream> #include<cstdio> using namespace std; bool IsPrime(int x) { for (int i = 2; i*i <= x; i++) if (x%i == 0) return false; return true; } int main() { int t, n; scanf("%d", &t); while (t–) { scanf("%d", &n); if (n == 2) printf("1n"); else if (n == 4) printf("2n"); else if (IsPrime(n)) printf("%dn", n – 1); else printf("0n"); } return 0; } [/cpp] 本代码提交AC,用时967MS,内存1576K。]]>

Leave a Reply

Your email address will not be published. Required fields are marked *