#P13406. [GCJ 2010 #3] Different Sum
[GCJ 2010 #3] Different Sum
题目描述
We have come up with a wonderful problem for Google Code Jam 2010 that involves contestants solving a cryptarithm. But we need your help in creating testcases for the problem; more precisely, we're concerned with addition equations that are good enough (in the sense defined below) for conversion into cryptarithms.
You don't need to know what a cryptarithm is to solve this problem, as we'll provide all required definitions. We define a cryptarithm equation to be an addition equation written in such a way that all summands (numbers being added) and the sum are aligned to the same right border like this:
124
31
25
---
180
Additionally, for each column of a cryptarithm equation, all digits of the summands in that column must be different. Note that we don't include the sum in this constraint. So for example in the above equation the first column contains only digit , the second column contains digits and , and the third column contains digits and . This equation is not a cryptarithm equation since the second column contains two 's. However, it would be a cryptarithm equation if we replaced the last summand with (and the sum with ).
Note that summands in a cryptarithm equation are always positive and written without leading zeros. The order of summands is not important (in other words, two equations which differ only in the order of the summands are considered the same).
The example above was in base , but we're also interested in cryptarithm equations in other bases. Note that a "digit" in base could mean any integer between and . Here is a cryptarithm equation in base :
I7B
JJJ
----
1F47
In this example, "I" stands for digit , "B" stands for digit , "J" stands for digit , and "F" stands for digit . In decimal notation, the two summands are and , and the sum is $1\times 23^3 + 15\times 23^2 + 4\times 23 + 7 = 20201$. Please note that denoting digits of and more with letters was done purely for the clarity of the example; it doesn't really matter in this problem how exactly we denote such digits in writing.
How many cryptarithm equations are there with the given sum in the given base ?
Since the answer might be very large, please output it modulo .
输入格式
The first line of the input gives the number of test cases, . lines follow. Each contains two positive integers and . All input numbers are given in base .
输出格式
For each test case, output one line containing "Case #: ", where is the case number (starting from ) and is the number of different cryptarithm equations with the given sum. Since this number can be very big, please output it modulo . Of course, the output itself should be in base .
2
6 10
8 4
Case #1: 4
Case #2: 4
提示
Sample Explanation
Here are the cryptarithm equations with sum :
6 1 2 1
- 5 4 2
6 - - 3
6 6 -
6
And here are the cryptarithm equations in base with sum :
20 11 13 10
-- 3 1 3
20 -- -- 1
20 20 --
20
Limits
- .
Small dataset (7 Pts, Test set 1 - Visible)
- Time limit:
303 seconds. - .
- .
Large dataset (22 Pts, Test set 2 - Hidden)
- Time limit:
12020 seconds. - .
- .