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 M keys. 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
4
1 1
3 4
5 5
15 15

Output

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

google在线笔试题。这题一直没做出来。有人说有公式,看到大牛们提交的代码又觉得像是dp。后来学了生成函数之后,觉得应该是一道指数型生成函数的题。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4
 5 using namespace std;
 6 const double epi = 0.000001;
 7 int frac(int k) {
 8     int ans = 1;
 9     for (int i = 2; i <= k; ++i) {
10         ans *= i;
11     }
12     return ans;
13 }
14
15 int enumPassword(int n, int m) {
16     vector<vector<double> > params(2, vector<double>(n + 1, 0));
17     params[0][0] = 1;
18     int cur = 0, next = 1;
19
20     for (int i = 0; i < m; ++i) {
21         params[next].assign(n + 1, 0);
22         for (int j = 0; j <= n; ++j) {
23             if (params[cur][j] < epi) continue;
24             for (int k = 1; k + j <= n; ++k) {
25                 params[next][k + j] = params[next][k + j] + params[cur][j] * 1 / frac(k);
26             }
27         }
28         cur = !cur; next = !next;
29     }
30
31     return params[cur][n] * frac(n);
32 }
33
34 int main(int argc, char** argv) {
35     if (argc < 2) return -1;
36     freopen(argv[1], "r", stdin);
37     int test;
38     scanf("%d", &test);
39     for (int i = 0; i < test; ++i) {
40         int m, n;
41         scanf("%d%d", &m, &n);
42         cout << "Case #" << i + 1 << ": " << enumPassword(n, m) << endl;
43     }
44
45     return 0;
46 }

但是数太大,要取模。除操作不能直接除模。

在网上搜到一个定理:

定理12.2:设$a_n$,$b_n$的指数生成函数分别为f(x)和g(x),则:

$f(x)*g(x) = \sum_{n=0}^{\infty}c_n\frac{x^n}{n!}, c_n = \sum_{k=0}^{n}C(n,k)a_kb_{n-k}$。

对应到我们这里,Line 25里就变成params[cur][j]*1*C(j+k, k),params[cur][j]对应的是$\frac{x^j}{j!}$的系数,1对应的是$\frac{x^k}{k!}$,所以乘以的就是C(j+k, k)了。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4
 5 using namespace std;
 6 enum {MOD = 1000000007};
 7 typedef long long llong;
 8 llong combination[100][100];
 9 void getCombination() {
10     for (int i = 0; i <= 100; ++i) {
11         for (int j = 0; j <= i; ++j) {
12             if (j == 0) {
13             combination[i][j] = 1;
14             } else {
15                 combination[i][j] = (combination[i - 1][j] + combination[i - 1][j - 1]) % MOD;
16             }
17         }
18     }
19 }
20
21 llong enumPassword(int n, int m) {
22     vector<vector<llong> > params(2, vector<llong>(n + 1, 0));
23     params[0][0] = 1;
24     int cur = 0, next = 1;
25
26     for (int i = 0; i < m; ++i) {
27         params[next].assign(n + 1, 0);
28         for (int j = 0; j <= n; ++j) {
29             if (params[cur][j] == 0) continue;
30             for (int k = 1; k + j <= n; ++k) {
31                 params[next][k + j] = (params[next][k + j] + params[cur][j] * combination[j + k][k]) % MOD;
32             }
33         }
34         cur = !cur; next = !next;
35     }
36
37     return params[cur][n];
38 }
39
40 int main(int argc, char** argv) {
41     if (argc < 2) return -1;
42     freopen(argv[1], "r", stdin);
43     if (argc >= 3) freopen(argv[2], "w", stdout);
44     getCombination();
45     int test;
46     scanf("%d", &test);
47     for (int i = 0; i < test; ++i) {
48         int m, n;
49         scanf("%d%d", &m, &n);
50         //cout << "Case #" << i + 1 << ": " << enumPassword(n, m) << endl;
51         printf("Case #%d: %lld\n", i + 1, enumPassword(n, m));
52     }
53
54     return 0;
55 }

注意这里求组合数要用递推公式来求,这样可以在运算中取模,避免溢出。

$C(n, m) = C(n - 1, m) + C(n - 1, m - 1)$。

以后指数型生成函数的题都可以这么做。get!

时间: 2024-12-15 01:34:35

Password Attacker的相关文章

N球M盒问题 &amp; Password Attacker google code jam

第一个题目本质是个动归dp问题.但是分析dp的方程使用了组合数学中的N求M盒问题. 先来看下N球M盒的经典问题. 给定N个相同的球,放入M个不同的盒子中,要求每个盒子必须非空,求组合数. 假设Xi为第i个盒子中的放入的小球数,则方程 X1+X2+X3+...+Xm=N, 其实抽象成数学问题,就是求这个M元1次方程的正整数解的组数. 插板法分析: N个小球排成一行,插入M-1个板,只能在小球中间插,而且板与板之间必须有球 分析到这里,聪明的大家肯定一看就知道是个N-1个空隙选M-1个来放入板,Cm

A complex 16-Level XSS Challenge

A complex 16-Level XSS Challenge, held in summer 2014 (+1 Hidden Level) Index Level 0 Level 1 Level 2 Level 3 Level 4 Level 5 Level 6 Level 7 Level 8 Level 9 Level 10 Level 11 Level 12 Level 13 Level 14 Level 15 Hidden Level Rules Call prompt(1) to w

【DP】组合数字

Password Attacker 题意就是给 M 个关键字,组合成 N 字符长度的结果,每一个关键字都必须在 N 位的字符中出现,有多少种可能结果. 范围 1 ≤ M ≤ N ≤ 100. 举例假设 M = 3(key = 3, 7, 5)  N = 4 位字符长度 结果可以为3577, 3557, 7353, 5735. 下面的结果是不合法的 1357 // 1 没有在key中,不是合法关键字 3355 // 7 是关键字,但是结果中没有出现 357 // 结果必须是一个4位长度 思路1:

ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password

What I don't fully understand is the use of ClientId and Secret vs Username and Password. The code I pasted generates a token by username and password and I can work with that token (until it expires), but when I try to get a refresh token, I must ha

输入password登录到主界面,录入学生编号,排序后输出

n 题目:输入password登录到主界面,录入学生编号,排序后输出 n 1.  语言和环境 A.实现语言 C语言 B.环境要求 VC++ 6.0 n 2.  要求 请编写一个C语言程序.将若干学生编号按字母顺序(由小到大)输出. 程序的功能要求例如以下: 1)  输入password"admin",正确则进入主界面,错误则直接推出(exit(0)): 2)从键盘输入5个学生编号"BJS1001","BJS2001"."BJS1011&

关于JavaEE项目连接数据库提示 Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)的解决方案

最近这三天,都在解决如下的问题:我MyEclipse中的JavaEE工程,运行之后就提示Access denied for user 'root'@'localhost' (using password: YES),当我用一个Java文件连接MySQL数据库时,没有任何问题.于是我在网上找了一些解决方案: (1)让root给当前用户授予增删改查的权限:grant select,insert,update,delete on *.* to 用户名@"%" Identified by &q

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password October 11, 2014 by sharad chhetri 4 Comments In this post we will learn, how to reset / recover forgotten root password on CentOS 7 / RHEL 7 (Red Hat Enterprise Linux 7). On RHEL 5/6 or Cen

Reset root password in CentOS 7 / RHEL 7

There may be some occasion you will end up with requirement to reset the root password, the occasion comes when you forget root password; follow this guide to reset the password of root. In CentOS, single user mode will help us to achieve our goal of

How To Reset Your Forgotten Root Password On CentOS 7 Servers

Sometimes you forget stuff. I do. I forget important passwords for important websites sometimes. Retrieving your forgotten passwords for most websites is easy, all one has to do remember few details that were used when signing up for the service to g