您的位置:首页 > 编程语言 > Go语言

Google APAC test 2015 Round B Problem A - Password Attacker

2014-09-16 12:40 316 查看

Problem A. Password Attacker

Passwords are widely used in our lives: for ATMs, online forum logins, mobile device unlock and door access. Everyone cares about password security. However, attackers always find ways to steal our passwords.
Here is one possible situation:
Assume that Eve, the attacker, wants to steal a password from the victim Alice. Eve cleans up the keyboard beforehand. After Alice types the password and leaves, Eve collects the fingerprints on the keyboard.
Now she knows which keys are used in the password. However, Eve won't know how many times each key has been pressed or the order of the keystroke sequence.
To simplify the problem, let's assume that Eve finds Alice's fingerprints only occurs on Mkeys. And she knows, by another method, that Alice's password contains N characters.
Furthermore, every keystroke on the keyboard only generates a single, unique character. Also, Alice won't press other irrelevant keys like 'left', 'home', 'backspace' and etc.
Here's an example. Assume that Eve finds Alice's fingerprints on M=3 key '3', '7' and '5', and she knows that Alice's password is N=4-digit in length. So all the following
passwords are possible: 3577, 3557, 7353 and 5735. (And, in fact, there are 32 more possible passwords.)
However, these passwords are not possible:
1357  // There is no fingerprint on key '1'
3355  // There is fingerprint on key '7',
so '7' must occur at least once.
357   // Eve knows the password must be a 4-digit number.

With the information, please count that how many possible passwords satisfy the statements above. Since the result could be large, please output the answer modulo 1000000007(109+7).

Input

The first line of the input gives the number of test cases, T.

For the next T lines, each contains two space-separated numbers M and N, indicating a test case.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the total number of possible passwords modulo 1000000007(109+7).

Limits

Small dataset

T = 15.

1 ≤ M ≤ N ≤ 7.

Large dataset

T = 100.

1 ≤ M ≤ N ≤ 100.

Sample

Input 

 
Output 

 
4
1 1
3 4
5 5
15 15

Case #1: 1
Case #2: 36
Case #3: 120
Case #4: 674358851


题意:
某密码的长度为N,已知它由M个不同字符组成(每一个都不能少,可出现重复),求密码可能的种数?
分析:
该题等价于将N个不同的小球放到M个不同的盒子里,不允许出现空盒,是典型的组合数学问题。
M个不同的字符代表M个不同的盒子,位置号1,2,...,N代表N个不同的小球的编号,小球j放到盒子i表示密码的第i为是第j个字符

该组合数学问题的值是M!*S(N,M),其中S(N,M)是斯特林数,递推关系如下:
S(N,M) = M*S(N-1,M) + S(N-1,M-1) ,1<= M <=N-1
        S(N,1) = 1, S(N,M) = 1

代码:

def values(Res, n, m):
if Res
[m] != -1:
return Res
[m]
if m==1 or m == n:
Res
[m] = 1
return 1
Res
[m] = values(Res, n-1, m-1)+m*values(Res, n-1, m)
return Res
[m]

def Numbers(m, n):
Res = []
for i in range(0, n+2):
res0 = [-1]*(m+2)
Res.append(res0)

stage = 1
for i in range(1,m+1):
stage = stage*i

return (values(Res, n, m)*stage) % 1000000007

fin = open('A_large_data.in','r')

fout = open('A_large_data.out','w')

line = fin.readline()

n = int(line)

for i in range(0,n):
line = fin.readline()
line = line.split(' ')
m = int(line[0])
n = int(line[1])
res = Numbers(m, n)
fout.write("Case #%d: %s\n"%(i+1, res))

fin.close()

fout.close()

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息