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 ...
随机推荐
- Codeforces Round #371 (Div. 2) - A
题目链接:http://codeforces.com/contest/714/problem/A 题意:有两个人A,B 给定A的时间区间[L1,R1], B的时间区间[L2,R2],然后在正好K分钟的 ...
- Swift3.0语言教程字符串与文件的数据转换
Swift3.0语言教程字符串与文件的数据转换 Swift3.0语言教程字符串与文件的数据转换,如果想要对字符串中的字符进行永久保存,可以将字符串中的字符写入到文件中.当然,开发者也可以将写入的内容进 ...
- maven 各种用途
1.maven 管理项目编译 作为项目编译代码管理工具,可以方便的进行编译集成. 2. maven 扩展单元测试 扩展对接junit可以方便进行单元测试 3.maven profiles各种devel ...
- ajax上传后用超链接展示无法下载问题
ajax插件上传后用超链接展示出来,但是点击超链接无法下载,最后发现是上传文件名为中文在作怪,于是修改了tomcat配置文件server.xml中的 <Connector port=" ...
- spring4 mvc 出错
java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetada ...
- 疯狂java学习笔记之面向对象(五) - 封装、继承、多态
一.封装: 封装的概念: - 合理的隐藏:隐藏不想被外界操作的Field.方法.构造器 - 合理的暴露:一般就是希望给别人调用的方法 e.g:显示器(按键暴露出来操作,但实际的东西/细节方法被隐藏起来 ...
- C# 词法分析器(二)输入缓冲和代码定位
系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 一.输入缓冲 在介绍如何进行词法分析之前,先来说说一 ...
- CodeForces#275--DIV 2--A
A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Mishka and Divisors[CodeForces Round #365 Div.2]
http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...
- [转]使用EasyRsa3为OpenVPN生成密码
1. 下载Easy RSA3 下载完并解压后,拷贝一份到/etc/openvpn和/home/client下 #.3版本需要独立下载个easy-rsa,该包用来制作ca证书,服务端证书,客户端证书 w ...