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。后来学了生成函数之后,觉得应该是一道指数型生成函数的题。

 #include <iostream>
#include <cstdio>
#include <vector> using namespace std;
const double epi = 0.000001;
int frac(int k) {
int ans = ;
for (int i = ; i <= k; ++i) {
ans *= i;
}
return ans;
} int enumPassword(int n, int m) {
vector<vector<double> > params(, vector<double>(n + , ));
params[][] = ;
int cur = , next = ; for (int i = ; i < m; ++i) {
params[next].assign(n + , );
for (int j = ; j <= n; ++j) {
if (params[cur][j] < epi) continue;
for (int k = ; k + j <= n; ++k) {
params[next][k + j] = params[next][k + j] + params[cur][j] * / frac(k);
}
}
cur = !cur; next = !next;
} return params[cur][n] * frac(n);
} int main(int argc, char** argv) {
if (argc < ) return -;
freopen(argv[], "r", stdin);
int test;
scanf("%d", &test);
for (int i = ; i < test; ++i) {
int m, n;
scanf("%d%d", &m, &n);
cout << "Case #" << i + << ": " << enumPassword(n, m) << endl;
} return ;
}

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

在网上搜到一个定理:

定理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)了。

代码如下:

 #include <iostream>
#include <cstdio>
#include <vector> using namespace std;
enum {MOD = };
typedef long long llong;
llong combination[][];
void getCombination() {
for (int i = ; i <= ; ++i) {
for (int j = ; j <= i; ++j) {
if (j == ) {
combination[i][j] = ;
} else {
combination[i][j] = (combination[i - ][j] + combination[i - ][j - ]) % MOD;
}
}
}
} llong enumPassword(int n, int m) {
vector<vector<llong> > params(, vector<llong>(n + , ));
params[][] = ;
int cur = , next = ; for (int i = ; i < m; ++i) {
params[next].assign(n + , );
for (int j = ; j <= n; ++j) {
if (params[cur][j] == ) continue;
for (int k = ; k + j <= n; ++k) {
params[next][k + j] = (params[next][k + j] + params[cur][j] * combination[j + k][k]) % MOD;
}
}
cur = !cur; next = !next;
} return params[cur][n];
} int main(int argc, char** argv) {
if (argc < ) return -;
freopen(argv[], "r", stdin);
if (argc >= ) freopen(argv[], "w", stdout);
getCombination();
int test;
scanf("%d", &test);
for (int i = ; i < test; ++i) {
int m, n;
scanf("%d%d", &m, &n);
//cout << "Case #" << i + 1 << ": " << enumPassword(n, m) << endl;
printf("Case #%d: %lld\n", i + , enumPassword(n, m));
} return ;
}

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

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

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

Password Attacker的更多相关文章

  1. [GCJ]Password Attacker

    https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...

  2. 【DP】组合数字

    Password Attacker 题意就是给 M 个关键字,组合成 N 字符长度的结果,每一个关键字都必须在 N 位的字符中出现,有多少种可能结果. 范围 1 ≤ M ≤ N ≤ 100. 举例假设 ...

  3. hdu3625

    hdu3625 题意: 酒店发生一起谋杀案.作为镇上最好的侦探,您应该立即检查酒店的所有N个房间.但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能 ...

  4. 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 ...

  5. bitbar 网站攻击实验

    实验环境 https://github.com/TouwaErioH/security/tree/master/web1 Windows10 Oracle VM VirtualBox Ubuntu16 ...

  6. Spring Security(三十三):10.3 Password Encoding

    Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...

  7. 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 ...

  8. MetInfo Password Reset Poisoning By Host Header Attack

    if we know some user's email, the we will can reset the user's email by host header attack. The atta ...

  9. 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?

    p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...

随机推荐

  1. HTTP基础07--认证

    何为认证 BASIC 认证 是从 HTTP/1.0 就定义的认证方式.即便是现在仍有一部分的网站会使用这种认证方式.是 Web 服务器与通信客户端之间进行的认证方式. 步骤 1: 当请求的资源需要 B ...

  2. vs2008/2010安装无法打开数据文件解决方案

    本人在安装VS2008或2010时,在开始的第一个页面(进度条大约加载到75%左右),提示“无法打开数据文件 'C:/Documents and Settings/Administrator/Loca ...

  3. [bzoj1071]组队[单调指针乱搞]

    这道题也很感人,主要改了比较久的时间... bzoj第一页的题,居然只过了五百多个人,(我是第512,orzliyicheng是513) 代码不长,但是细节搞了很久,主要sort写错了,晕... 首先 ...

  4. Mac 自带 apache

    1.启动服务器 sudo apachectl start http://localhost   能访问就ok sudo apachectl stop sudo apachectl restart ht ...

  5. 【原】iOS学习之ARC和非ARC文件混编

    在编程过程中,我们会用到很多各种各样的他人封装的第三方代码,但是有很多第三方都是在非ARC情况下运行的,当你使用第三方编译时出现和下图类似的错误,就说明该第三方是非ARC的,需要进行一些配置.

  6. BZOJ4421 : [Cerc2015] Digit Division

    如果两个相邻的串可行,那么它们合并后一定可行,所以求出所有可行的串的个数$t$,则$ans=2^{t-1}$. 注意特判整个串不可行的情况,这个时候答案为0. #include<cstdio&g ...

  7. ACM:统计难题 解题报告-字典树(Trie树)

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. 为什么C#不使用多继承?(from stackoverflow)

    简单地说:是因为该语言的设计者决定不使用. 基本上,.NET和Java的设计者不使用多继承(MI),是因为他们认为给语言加上多继承获得的好处较少,抵不上因此增加的复杂性. 1.不同的语言对于多继承如何 ...

  9. 预定义指令之debug

    1)根据你必须知道的.NET一书, #define DEBUG class Program { static void Main(string[] args) { #if DEBUG Console. ...

  10. Crystal Reports 2008(水晶报表) 第一个报表

    学习Craystal Reports 2008的时候,光看说明文档,很多东西看了就忘了. 我在看文档的时候,是跟着文档上面来做的. 这样边看边做,效果还不错哈 下面就是我的第一个demo 先看看效果: ...