2014 Programming Beauty-Qualifying-great God with three small partners

Source: Internet
Author: User

Topic 2: Great God and three small partners time limit: 2000ms single point time limit: 1000ms memory limit: 256MB description

L Country is a beautiful scenery and rich country, many people like to come here to travel and like to take away some souvenirs, great God classmate is no exception. The time to open the country more and more close, the great God classmate is worrying to her lovely little friends with what souvenirs good, now put in front of the great God classmate has three kinds of souvenirs A, B, C can choose, each kind of souvenirs have n species. The types of a_i, b_i, c_i souvenirs of the value are I, and respectively have n+1-i surplus. Now the great God students want to choose one of the three types of souvenirs and then give her three lovely small partners, but she did not want to just pick out two pieces of the same value of souvenirs, because the same value souvenir of the two small partners will think that the great God classmate partiality to another small partner and ignore her for more than a week. Now, the great God classmate hope you buy three pieces of souvenirs can make three small partners happy and not with her uncomfortable, she wants to know how many different methods of selection?

Because the number of scenarios may be very large, the great God students want to know the choice of souvenirs of the program number model 10^9+7 after the answer.

Input

The first line includes a number T, which represents the number of groups of data.

Next contains the T-group data, one row for each group of data, including an integer n.

Output

For each set of data, the output line "case x:", where x represents the number of each group of data (starting from 1), followed by a number, indicating the number of options after the Die 10^9+7.

Data range

Small Data:

1<=t<=10

1<=n<=100

Big Data:

1<=t<=1000

1<=n<=10^18

Sample explanation

For the second set of data, the legal scheme has the following, (x, Y, z) indicates that the value of Category A souvenir is X, the value of category B souvenir is Y, and the value of category C souvenir is Z.

(1,1,1): 3*3*3=27 species

(Three-way): 3*2*1=6

(1,3,2): 3*1*2=6 species

(2,1,3): 2*3*1=6 species

(2,2,2): 2*2*2=8 species

(2,3,1): 2*1*3=6 species

(3,1,2): 1*3*2=6 species

(3,2,1): 1*2*3=6 species

(3,3,3): 1*1*1=1 species

A total of 27+6+6+6+8+6+6+6+1=72 options for souvenir selection

Note that, such as (1,1,2), (2,3,3), (3,1,3) because of the exact choice of two pieces of the same value of souvenirs, it is not a matching method of souvenir selection.

Sample input
213
Sample output
Case 1:1case 2:72

Calculate all cases First

(1+2+...+n) ^3 = (n (n+1)/2) ^3

Exclude two or more of the same value

(1^2+2^2+3^2+...+n^2) * (1+2+...+n)

And because 1^2+2^2+3^3+...+n^2=n* (n+1) * (2*n+1)/6,

SO (1^2+2^2+3^2+...+n^2) * (1+2+...+n) *3=n* (n+1) * (2*n+1)/6*n* (n+1)/2*3

Put three of the same add back

1^3+2^3+...+n^3 = (1+2+...+n) ^2 = (n (n+1)/2) ^2

But note that these are counted 3 times in the 2nd case!

So multiply by three!

After simplification for (n (n+1)/2) ^3-n*n* (n+1) * (n+1) * (n-1)/2

And then think of is how to calculate this formula, multiplication when there is the denominator, general processing such cases will use inverse, but this topic is a bit special n max for 10^18, then will not explode long long,n*n will explode unsigned long long, according to the above formula You can think of odd and even discussions.
1#include <iostream>2#include <algorithm>3#include <queue>4#include <cstring>5#include <cstdio>6#include <vector>7#include <string>8#include <iterator>9#include <cmath>Ten#include <deque> One#include <stack> A#include <cctype> - using namespacestd; -  thetypedefLong Longll; -typedefLong Doubleld; -  - Const intN = -+Ten; + Const intINF =0xFFFFFFF; - Const DoubleEPS = 1e-8; + Constll MOD = 1e9 +7; A  at #defineINFL 0X7FFFFFFFFFFFFFFFLL - #defineMet (A, b) memset (A, B, sizeof (a)) - #defineRep (c, a, b) for (ll C = A; C < b; C + +) - #defineNRE (c, a, b) for (int c = A; C > b; c--) -  //(n (n+1)/2) ^3-n*n* (n+1) * (n+1) * (n-1)/2 - intMain () in { -     intT, k =1; toCIN >>T; +      while(t--) -     { the ll A, b, N; *CIN >>N; $cout <<" Case"<< k++ <<": ";Panax Notoginseng         if(N &1) A = n MOD, B = (n +1) /2%MOD; -         ElseA = N/2% MOD, B = (n +1) %MOD; thell tmp = A * b%MOD; +ll ans = tmp * tmp% MOD *tmp; ATMP = tmp * (n MoD)% mod * ((n +1)% MoD)% mod * ((N-1)% MOD)%MOD; thecout << (ans-tmp + MoD)% mod <<Endl; +     } -     return 0; $}
View Code

Multiplication Inverse definition:
The k value satisfying a*k≡1 (mod p) is a multiplicative inverse of p.

Why do we have to multiply the inverse element?
When we ask for (A/b) mod P's value, and a is large, and cannot directly obtain a A/b value, we will use the multiplication inverse.
We can use the B to multiply the inverse k of p, multiply a by the K-mode p, ie (a*k) mod p. The result is equivalent to (A/b) mod p.

Card: (actually very simple ...) )
According to B*k≡1 (mod p) there is b*k=p*x+1.
k= (p*x+1)/b.
Put K into (a*k) mod p, get:
(* (p*x+1)/b) MoD p
= ((a*p*x)/b+a/b) mod p
=[((a*p*x)/b) mod p + (A/b)] mod p
=[(p* (a*x)/b) mod p + (A/b)] mod p
p*[(a*x)/b] MoD p=0
So the original equals: (A/b) mod p

The multiplication inverse is used when calculating a large number of MODP. That is, the/a becomes * (f (a)), where F (a) is a multiplicative inverse of a in modulo P meaning, i.e. a*f (a) mod p=1

There are two methods for calculating the multiplication inverse, extending the GCD or the fast power modulo based on Euler's formula.

------------------------------------------------------------------------------------------------------

The extended GCD is the smallest integer solution to solve the equation Ax=1 (mod P).

Set ax=1+y*p, i.e. a*f (a,p) =1+p*g (a,p), to consider the solution of X and Y as a function of a,p.

When A>=p, set a=p*k+r, the equation becomes:

P*k*f (a,p) +r*f (a,p) =1+p*g (a,p), move item, get R*f (a,p) =1+p* (g (a,p)-k*f (a,p))

Then get f (a mod p,p) =f (a,p), g (a mod p,p) =g (a,p)-[a/p]*f (a,p);

P>=a is almost a meaning.

So the f,g is set to a global variable, like ordinary gcd do, instant update f,g can.

----------------------------------------------------------------------------------------------------

Set Euler function phi (x) = number of digits in [1,x-1] with x coprime. So Euler's formula:

A^phi (P) =1 (mod p)

Both sides with a, you can get a^ (Phi (P)-1) mod p is a in the meaning of modulo p multiplication inverse.

Exception:

If P is a prime number, then Phi (p) =p-1. That is, the multiplication inverse of A is a^ (P-2), the fast power can be.

2014 Programming Beauty-Qualifying-great God with three small partners

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.