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

Tower

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3407    Accepted Submission(s): 821


Problem Description
Alan loves to construct the towers of building bricks. His towers consist of many cuboids with square base. All cuboids have the same height h = 1. Alan puts the consecutive cuboids one over another:

Recently in math class, the concept of volume was introduced to Alan. Consequently, he wants to compute the volume of his tower now. The lengths of cuboids bases (from top to bottom) are constructed by Alan in the following way:

1. Length a1 of the first square is one.

2. Next, Alan fixes the length a2 of the second square.

3. Next, Alan calculates the length an (n > 2) by 2*a2*(an-1)-(an-2). Do not ask why he chose such

a formula; let us just say that he is a really peculiar young fellow. For example, if Alan fixes a2 = 2, then a3 = 8 -a1 = 7; see Figure 1. If Alan fixes a2 = 1, then an = 1 holds for all n belong to N; see Figure 2.

Now Alan wonders if he can calculate the volume of tower of N consecutive building bricks. Help Alan and write the program that computes this volume. Since it can be quite large, it is enough to compute the answer modulo given natural number m.
 

Input
The input contains several test cases. The first line contains the number t (t <= 10^5) denoting the number of test cases. Then t test cases follow. Each of them is given in a separate line containing three integers a2,N,m (1 <= a2,m <= 10^9, 2 <= N <= 10^9) separated by a single space, where a2 denotes the fixed length of second square in step 2, while N denotes the number of bricks constructed by Alan.
 

Output
For each test case (a2,N,m) compute the volume of tower of N consecutive bricks constructed by Alan according to steps (1-3) and output its remainder modulo m.
 

Sample Input
3 2 3 100 1 4 1000 3 3 1000000000
 

Sample Output
54 4 299
 

Hint


 

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-04-17 04:06:15, Gzip enabled