F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Dividing This Product

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 514    Accepted Submission(s): 112


Problem Description
For given positive integer $n$, suppose that $p_1 = 2 < p_2 = 3 < \cdots < p_r \le n$ are all of the primes no larger than $n$. Let $P(n) = p_1p_2\cdots p_r+1$ and let $p$ be a prime dividing $P$; then $p$ can not be any of $p_1,p_2,\cdots,p_r$, otherwise $p$ would divide the difference $P(n)-p_1p_2\cdots p_r=1$, which is impossible. So this prime $p$ is still another prime, and $p_1, p_2, \cdots, p_r$ would not be all of the primes.

It is a common mistake to think that this proof says the product $P(n)=p_1p_2\cdots p_r+1$ is prime. The proof actually only uses the fact that there is a prime dividing this product.

Further considerations need you to compute $\frac{P(n)-1}{M}$, modulo $M$. We guarantee that $M$ is a prime number.
 

Input
The input contains several test cases. The first line of the input is a single integer $t~(1\le t\le 5)$ which is the number of test cases. $t$ test cases follow.
Each test case contains two positive integers $n~(M\le n\le 5\times 10^8)$ and $M~(3\le M\le 2000)$.

The sum of $n(s)$ for all test cases would not be larger than $5\times 10^8$.
 

Output
For each test case, you should output $\frac{P(n)-1}{M}$ module $M$.
 

Sample Input
10 13 13 17 13 19 13 23 13 29 13 1300 13 1700 13 1900 13 2300 13 2900 13
 

Sample Output
Case #1: 9 Case #2: 10 Case #3: 8 Case #4: 2 Case #5: 6 Case #6: 4 Case #7: 8 Case #8: 11 Case #9: 2 Case #10: 9
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 12:35:56, Gzip enabled