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。后来学了生成函数之后,觉得应该是一道指数型生成函数的题。
#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的更多相关文章
- [GCJ]Password Attacker
https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...
- 【DP】组合数字
Password Attacker 题意就是给 M 个关键字,组合成 N 字符长度的结果,每一个关键字都必须在 N 位的字符中出现,有多少种可能结果. 范围 1 ≤ M ≤ N ≤ 100. 举例假设 ...
- hdu3625
hdu3625 题意: 酒店发生一起谋杀案.作为镇上最好的侦探,您应该立即检查酒店的所有N个房间.但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能 ...
- 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 ...
- bitbar 网站攻击实验
实验环境 https://github.com/TouwaErioH/security/tree/master/web1 Windows10 Oracle VM VirtualBox Ubuntu16 ...
- Spring Security(三十三):10.3 Password Encoding
Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...
- 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 ...
- 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 ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
随机推荐
- 《DSP using MATLAB》示例Example4.10
上代码: b = [1, 0.4*sqrt(2)]; a = [1, -0.8*sqrt(2), 0.64]; % compute the polynomials coefficients given ...
- js-面向对象的程序设计,函数表达式
面向对象的程序设计: 1.属性类型:数据属性.访问器属性 数据属性:wirtable:false –只读:如果尝试为它赋值,会忽略 Configurable:false—不能从对象中删除属性 在调用O ...
- 创建com服务器
Delphi Com深入编程 第二章:
- 软件打开时间、窗体透明度、背景色---《用delphi开发共享软件》-15.1任务管理器
1.计算软件启动了多长时间:用定时器,每分钟触发一次: procedure TFrmMain.tmCheckLegalTimer(Sender: TObject);Var Minutes:LongIn ...
- git merge 与 rebase 的区别
http://gitbook.liuhui998.com/4_2.html merge rebase
- unity mathf.repeat 截取操作
截取操作,可用于浮点数. Mathf.Repeat(Time.realtimeSinceStartup, 3*blinkTime) > blinkTime;
- Codeforces Round #243 (Div. 2) A. Sereja and Mugs
#include <iostream> #include <vector> #include <algorithm> #include <numeric> ...
- Coder-Strike 2014 - Round 1 E. E-mail Addresses
此题题意就是匹配邮箱,提交时一直在test 14上WA,看了测试用例之后才发现计数用的int溢出,要用long long还是做题经验不够,导致此题未能通过,以后一定要考虑数据量大小 题意是找出邮件地址 ...
- ACM Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- Codeforces Beta Round #8
A题,小小的模拟题,没看懂题意啊. #include <iostream> #include <cstdio> #include <cmath> #include ...