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
相关文章推荐
- Google APAC test 2015 Round B Problem B - New Years Eve
- Google APAC test 2015 Round B Problem C - Card Game
- Google APAC test 2015 Round B Problem D-Parentheses Order
- Google中国2015校园招聘笔试Round D APAC Test Problem A. Cube IV
- Google中国2015校园招聘笔试Round D APAC Test Problem B. GBus count
- Google中国2015校园招聘笔试Round D APAC Test Problem D. Itz Chess
- Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary
- Problem B. Safe Squares Google APAC 2017 University Test Round C
- Problem C. Evaluation Google APAC 2017 University Test Round C
- Problem A. Dynamic Grid Google APAC 2016 University Test Round D
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem B. Beautiful Numbers Google APAC 2017 University Test Round E
- Problem C. Codejamon Cipher Google APAC 2017 University Test Round D
- Problem C. Partioning Number Google APAC 2017 University Test Round E
- Problem A. gRanks Google APAC 2016 University Test Round C
- Problem A. Travel Google APAC 2016 University Test Round B
- Catalan数和应用 & 2015 google APAC round 2 problem D 括号配对问题
- [ Google APAC 2015 University Graduates Test ] Round C APAC Test
- Google 2015 APAC Round A Problem C - Addition
- Problem C. Jane's Flower Shop Google APAC 2017 University Test Round A