原来我还可以自己写黑题啊
给定10个效率低下的程序,请想办法将效率提高并得到给定输入的答案
Level 1
#include <stdio.h>
#include <stdlib.h>
void _() {
unsigned long long a, b, c, d, i;
scanf("%llu %llu %llu", &a, &b, &c);
i = 0;
d = 0;
while (i < b) {
d = d + a;
d = d % c;
i = i + 1;
}
printf("%llu\n", d);
}
int main() {
_();
_();
_();
_();
_();
_();
_();
_();
_();
_();
}开胃菜。
每轮+a,共+b轮,等于加ab。
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
void _() {
unsigned long long a, b, c, d, i;
scanf("%llu %llu %llu", &a, &b, &c);
i = 0;
d = 0;
d = d + a % c * (b%c) % c ;
printf("%llu\n", d);
}
int main() {
freopen("program1.in","r",stdin);
freopen("program1.out","w",stdout);
_();
_();
_();
_();
_();
_();
_();
_();
_();
_();
}Level 2
#include <stdio.h>
void _______() {
long long i, n, a, b, c, d, p;
a = 1;
b = 0;
c = 0;
scanf("%lld %lld", &n, &p);
i = 1;
while (i <= n) {
i = i + 1;
b = a + b;
a = 2 * b - a + c;
c = 2 * b - a + c;
while (a >= p) {
a -= p;
}
while (a < 0) {
a += p;
}
while (b >= p) {
b -= p;
}
while (b < 0) {
b += p;
}
while (c >= p) {
c -= p;
}
while (c < 0) {
c += p;
}
}
d = a - 2 * b + c;
while (d >= p) {
d -= p;
}
while (d < 0) {
d += p;
}
printf("%lld\n", d);
}
int main() {
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
return 0;
}神金作者在这里原先放了一个词典,光删除就累死我了。
这一组数据我考虑的是手模小数据,发现答案为
使用矩阵快速幂求斐波那契。
#include <stdio.h>
#include <cstdint>
#include <iostream>
using u64 = std::uint64_t;
using u128 = unsigned __int128;
long long p;
struct Mat {
u64 a00, a01, a10, a11;
};
Mat mul(const Mat& x, const Mat& y) {
return {
(u64)(((u128)x.a00 * y.a00%p + (u128)x.a01 * y.a10%p)%p),
(u64)(((u128)x.a00 * y.a01%p + (u128)x.a01 * y.a11%p)%p),
(u64)(((u128)x.a10 * y.a00%p + (u128)x.a11 * y.a10%p)%p),
(u64)(((u128)x.a10 * y.a01%p + (u128)x.a11 * y.a11%p)%p)
};
}
u64 fib_fast(u64 n) {
if (n == 0) return 0;
Mat res{1, 0, 0, 1};
Mat base{1, 1, 1, 0};
u64 e = n - 1;
while (e) {
if (e & 1) res = mul(res, base);
base = mul(base, base);
e >>= 1;
}
return res.a00;
}
void _______() {
long long i, n, a, b, c, d;
a = 1;
b = 0;
c = 0;
scanf("%lld %lld", &n, &p);
i = 1;
d=fib_fast(n-1)%p;
d=d*d%p;
printf("%lld\n", d);
}
int main() {
freopen("program2.in","r",stdin);
freopen("program2.out","w",stdout);
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
_______();
return 0;
}Level 3
#include <stdio.h>
unsigned long long s0, s1, s2, s3, s4, i, n;
int main() {
scanf("%llu", &n);
i = 0;
while (i <= n) {
s0 = s0 + 1;
s1 = s1 + i;
s2 = s2 + i * i;
s3 = s3 + i * i * i;
s4 = s4 + i * i * i * i;
i = i + 1;
}
printf("%llu\n", s0);
printf("%llu\n", s0);
printf("%llu\n", s1);
printf("%llu\n", s1);
printf("%llu\n", s2);
printf("%llu\n", s2);
printf("%llu\n", s3);
printf("%llu\n", s3);
printf("%llu\n", s4);
printf("%llu\n", s4);
return 0;
}使用公式即可
但是请不要用代码!代码还要判gcd等,十分复杂,不如计算器。
Level 4
#include <iostream>
const int N = 5000, inf = 0x3F3F3F3F;
int n, m, type;
bool data[N + 11][N + 11];
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
void generate_input(){
std::cin >> n >> m >> type;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
data[i][j] = bool((next_rand() % 8) > 0);
}
long long count1(){
long long ans = 0LL;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(data[i][j])
for(int k = 0; k < n; k++)
for(int l = 0; l < m; l++)
if(data[k][l] && (k != i || l != j))
ans++;
return ans;
}
int abs_int(int x){
return x < 0 ? -x : x;
}
long long count2(){
long long ans = 0LL;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(data[i][j]){
int level = inf;
for(int k = 0; k < n; k++)
for(int l = 0; l < m; l++)
if(!data[k][l]){
int dist = abs_int(k - i) + abs_int(l - j);
if(level > dist)
level = dist;
}
ans += level;
}
return ans;
}
int main(){
std::cin >> seed;
for(int i = 0; i < 10; i++){
generate_input();
std::cout << (type == 1 ? count2() : count1()) << std::endl;
}
return 0;
}看数据50005000,O()。
预处理不管
count1:统计1数对的数量,先求出共有q个1,答案即为
count2:计算每个1离最近的0的距离,使用bfs即可
从这里开始事情有点危险了,我改过之后跑了20s
#include <bits/stdc++.h>
const int N = 5000, inf = 0x3F3F3F3F;
int n, m, type;
bool data[N + 11][N + 11];
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
void generate_input(){
std::cin >> n >> m >> type;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
data[i][j] = bool((next_rand() % 8) > 0);
}
long long count1(){
long long ans = 0LL;
long long all=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(data[i][j])all++;
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(data[i][j])
ans+=all-1;
return ans;
}
int abs_int(int x){
return x < 0 ? -x : x;
}
typedef std::pair<int,int> P;
#define x first
#define y second
int dx[]={ 0,-1, 0, 1};
int dy[]={-1, 0, 1, 0};
int pos[N+11][N+11];
void bfs(){
std::queue<P>q;
while(!q.empty())q.pop();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
pos[i][j]=inf;
if(!data[i][j]){
q.push({i,j});
pos[i][j]=0;
}
}
}
while(!q.empty()){
P u=q.front();q.pop();
for(int i=0;i<4;i++){
int xx=dx[i]+u.x,yy=dy[i]+u.y;
if(xx<0||xx>=n||yy<0||yy>=m)continue;
int k=pos[u.x][u.y]+1;
if(k>=pos[xx][yy])continue;
pos[xx][yy]=k;
q.push({xx,yy});
}
}
}
long long count2(){
long long ans = 0LL;
bfs();
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(data[i][j]){
int level = pos[i][j];
ans += level;
}
return ans;
}
int main(){
freopen("program4.in","r",stdin);
freopen("program4.out","w",stdout);
std::cin >> seed;
for(int i = 0; i < 10; i++){
generate_input();
std::cout << (type == 1 ? count2() : count1()) << std::endl;
}
return 0;
}Level 5
#include <iostream>
const int N = 5011;
int n, m;
bool data[N][N];
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
void generate_input(){
std::cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
data[i][j] = bool((next_rand() % 8) > 0);
}
bool check(int x1, int y1, int x2, int y2){
bool flag = true;
for(int i = x1; i <= x2; i++)
for(int j = y1; j <= y2; j++)
if(!data[i][j])
flag = false;
return flag;
}
long long count3(){
long long ans = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
for(int k = i; k < n; k++)
for(int l = j; l < m; l++)
if(check(i, j, k, l))
ans++;
return ans;
}
int main(){
std::cin >> seed;
for(int i = 0; i < 10; i++){
generate_input();
std::cout << count3() << std::endl;
}
return 0;
}计算完全为1的矩形数量,使用单调栈,此段代码由AI生成。
#include <bits/stdc++.h>
using namespace std;
const int N = 5011;
int n, m;
bool data[N][N];
int seed;
int next_rand(){
static const int P = 1000000007, Q = 83978833, R = 8523467;
return seed = ((long long)Q * seed % P * seed + R) % P;
}
void generate_input(){
std::cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
data[i][j] = bool((next_rand() % 8) > 0);
}
long long countAllOnesSubmatrices(vector<vector<int>>& mat) {
int n = mat.size();
if (n == 0) return 0;
int m = mat[0].size();
vector<int> height(m, 0);
long long total = 0;
for (int i = 0; i < n; ++i) {
// 更新高度
for (int j = 0; j < m; ++j) {
if (mat[i][j] == 1) height[j] += 1;
else height[j] = 0;
}
// 计算当前行作为底边的全1子矩阵数量 = 所有子数组最小值的和
// 使用单调栈
vector<int> left(m), right(m);
stack<int> st;
// 左边第一个严格小于 height[j] 的位置
for (int j = 0; j < m; ++j) {
while (!st.empty() && height[st.top()] >= height[j]) st.pop();
left[j] = st.empty() ? -1 : st.top();
st.push(j);
}
// 右边第一个严格小于 height[j] 的位置
while (!st.empty()) st.pop();
for (int j = m - 1; j >= 0; --j) {
while (!st.empty() && height[st.top()] > height[j]) st.pop(); // 注意这里用 > 避免重复
right[j] = st.empty() ? m : st.top();
st.push(j);
}
for (int j = 0; j < m; ++j) {
long long cnt = (long long)(j - left[j]) * (right[j] - j);
total += (long long)height[j] * cnt;
}
}
return total;
}
long long count3(){
vector<vector<int>>mat(n,vector<int>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mat[i][j]=data[i][j];
}
}
long long ans=countAllOnesSubmatrices(mat);
return ans;
}
int main(){
freopen("program5.in","r",stdin);
freopen("program5.out","w",stdout);
std::cin >> seed;
for(int i = 0; i < 10; i++){
cerr<<i;
generate_input();
std::cout << count3() << std::endl;
}
return 0;
}Level 6
#include <stdio.h>
unsigned long long a, b, c, t, k, n;
unsigned long long rd() {
t = (t * t * a + b) % c;
return t;
}
int main() {
int i;
for (i = 0; i <= 9; i++) {
scanf("%llu %llu %llu %llu", &n, &a, &b, &c);
t = 0;
k = 1;
while (k <= n) {
k = k + 1;
rd();
}
printf("%llu\n", t);
}
}使用Brent循环节判环思想,但还是要400s左右,不过比10000h靠谱多了
什么?Floyd也能判这种环?我在学什么。
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("program6.in","r",stdin);
freopen("program6.out","w",stdout);
int i;
for (i = 0; i <= 9; i++) {
cerr<<i;
unsigned long long int n,a,b,c,s,t;
scanf("%llu %llu %llu %llu", &n, &a, &b, &c);
s=t=0;
int tn=0,tm=2;
while(true){//Brent的思想:倍增
t=(a*t*t+b)%c;
++tn;
if(s==t)break;
if(tn==tm){
tn=0;
tm<<=1;
s=t;
}
}
//tim_n->循环节长度
s=t=0;
int tim=0;
while(tim<tn){
t=(a*t*t+b)%c;
++tim;
}
tim=1;
while(s!=t){
s=(a*s*s+b)%c;
t=(a*t*t+b)%c;
++tim;
}
tm=(n-tim)%tn+1;
//tim->环的起点 s->起点的值
tim=1;
while(tim<=tm)s=(a*s*s+b)%c,++tim;
printf("%llu\n",s);
}
}Level 7
打开数据看一眼,再看程序是暴力解数独。懂了,解4个“字母独”!
这里我用了我的脑子!脑子被硬控10h
Level 8
#include <stdio.h>
unsigned long long a, b, c, d, e, f, g, n, q, r, s, t, u, v, w, x, y, z;
unsigned long long p = 1234567891;
int main() {
scanf("%llu", &n);
a = 0;
q = 0;
r = 0;
s = 0;
t = 0;
u = 0;
v = 0;
w = 0;
x = 0;
y = 0;
z = 0;
a = 0;
while (a < n) {
a = a + 1;
b = 0;
while (b < n) {
b = b + 1;
c = 0;
while (c < n) {
c = c + 1;
d = 0;
while (d < n) {
d = d + 1;
e = 0;
while (e < n) {
e = e + 1;
f = 0;
while (f < n) {
f = f + 1;
g = 0;
while (g < n) {
g = g + 1;
if (a < b && b < c && c < d && d < e && e < f && f < g) {
q = q + 1;
q = q % p;
}
if (a < b && c < g && c < d && e < f && a < d) {
r = r + 1;
r = r % p;
}
if (a < d && d < f && c < f && c < e && b < d) {
s = s + 1;
s = s % p;
}
if (d < e && b < d && a < f && d < e && b < g) {
t = t + 1;
t = t % p;
}
if (c < f && b < f && b < c && f < g && b < f) {
u = u + 1;
u = u % p;
}
if (b < d && b < c && d < f && c < e && b < e) {
v = v + 1;
v = v % p;
}
if (a < c && a < b && c < e && b < f && e < g) {
w = w + 1;
w = w % p;
}
if (b < d && b < f && a < g && c < g && a < e) {
x = x + 1;
x = x % p;
}
if (b < f && a < c && c < d && a < c && b < e) {
y = y + 1;
y = y % p;
}
if (d < e && e < f && a < d && c < g && b < d) {
z = z + 1;
z = z % p;
}
}
}
}
}
}
}
}
printf("%llu\n", q);
printf("%llu\n", r);
printf("%llu\n", s);
printf("%llu\n", t);
printf("%llu\n", u);
printf("%llu\n", v);
printf("%llu\n", w);
printf("%llu\n", x);
printf("%llu\n", y);
printf("%llu\n", z);
return 0;
}其实是组合数学,找十个等式。
#include <stdio.h>
unsigned long long n, q, r, s, t, u, v, w, x, y, z;
unsigned long long p = 1234567891;
unsigned long long ksm(unsigned long long a,
unsigned long long b){
unsigned long long ans=1;
while(b){
if(b&1)ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
}
int main() {
freopen("program8.in","r",stdin);
freopen("program8.out","w",stdout);
scanf("%llu", &n);
n%=p;
if(n>6)q=n*(n-1)%p*(n-2)%p*(n-3)%p
*(n-4)%p*(n-5)%p*(n-6)%p*ksm(5040,p-2)%p;
if(n>1)r=(n-1)*(n-1)%p*n%p*n%p*(2*n%p-1)%p
*(2*n%p*n%p-2*n%p+1+p)%p*ksm(60,p-2)%p;
if(n>2)s=n*n%p*(n-1)%p*(n-1)%p*(n-2)%p
*(2*n%p-1)%p*((7*n%p-3+p)%p)%p*ksm(360,p-2)%p;
if(n>2)t=n*n%p*n%p*(n-1)%p*(n-1)%p*(n-2)%p
*(3*n%p-1)%p*ksm(48,p-2)%p;
if(n>3)u=n*n%p*n%p*n%p*(n-1)%p*(n-2)%p*(n-3)%p
*ksm(24,p-2)%p;
if(n>2)v=n*n%p*n%p*(n-1)%p*(n-2)%p
*(3*n%p*n%p-6*n%p+1+p)%p*ksm(60,p-2)%p;
if(n>3)w=n*n%p*(n-1)%p*(n-2)%p*(n-3)%p
*(5*n%p*n%p-9*n%p+1+p)%p*ksm(360,p-2)%p;
if(n>1)x=n*n%p*(n-1)%p*(n-1)%p*(2*n-1)%p
*(5*n%p*n%p-5*n%p+2+p)%p*ksm(144,p-2)%p;
if(n>1)y=n*n%p*n%p*(n-1)%p*(n-1)%p*(n-2)%p
*(2*n-1)%p*ksm(36,p-2)%p;
if(n>3)z=n*n%p*(n-1)%p*(n-1)%p*(n-2)%p
*(n-3)%p*(2*n%p-3)%p*ksm(240,p-2)%p;
printf("%llu\n", q);
printf("%llu\n", r);
printf("%llu\n", s);
printf("%llu\n", t);
printf("%llu\n", u);
printf("%llu\n", v);
printf("%llu\n", w);
printf("%llu\n", x);
printf("%llu\n", y);
printf("%llu\n", z);
return 0;
}Level 9
/*
!o$&####&&########$o
!o*$&&##############&$o
!o*$$$&&###&&&#############&o
!*&######&&&##################&o
*&#############################$*ooo!
!&###################################$o!
!*&#####################################&o
o$########################################$o
!$&########################################&$!
!***$&######################################&$!
!$&&&&&###################################&*o
*$$$$&&###################################&$o!
****$&&&####################################&*o!
!**o**$&&####################################&&$*o!
oo*oo*$$&&##################################&$*oo!!
*oooo**$$&&##################################$o
oooo*o**$$&##################################&*!
oooo*****$&&#################################&*o!
***oo*****$&&&&&&###########################&*oo!
!$&$********$$$$&&&&&########################$o!
!o$&&&$***$$$$$$$$$$$&&######################&$!
!o***$$&&$$&&&&&&&$$$$$$$&#####################&*
o$*o$$$$$&$&&&&&&&&$$$$$$$&###################&*!
!*oo*&&&&&$$$$$$$$$$$$$$$&&&############&&&&#&*
!!o*$$&$$$$$$$$$$$$$&&&&&&#############&$&&&*
!***$**$$$$&&&&&&&###&&&&&##########&&$&&*!
!****oo****$$$&&&&&&&$$$$$&####&&&&&&$&&$!
!o$$*ooo******$$$$$$$$$$$$&&##&&&$&&$$$&$o
!$$*o****$$$$*$$$$$$$$$$$&&#&&$$$$$$$$*o
!*$$$$$*$$&&$$***$$$$$$$$&&&&&$$$***$$!
o$&&&&&&&&&&$$$$$$$$$$$$$$$$$$$**$&&*
*&&&&##&&&&&&$$$$$$$$$$$$$$$&&&&##$o
o*$$*$$$$$&&&&&&$$$$$$$$$$$$&&####$!
!****$$$$&&&$&&&$$&&$$$$$&&&&####*!
!**$$$&$$$$$$&&$&&&$&&&&&&&&&###$o
o$$$$$&$$$$$$&&&&&&&&&&&&&&&#&$!
o$&&&$&$$$$$&&&&&&&&&&&&&&&&&$*!
!*$$$$$$$$$$&&&&&&&&&&&&&$$$$$$$o!
!o*$***$$$$$&&&&&&&&&&&&$$$$$$$&&$!
o*$$$$$&&&&&&&&&&&&&&$$$$$$$$$$&#$o
!**$$$$&&#&&&&&&&&$$$$$$$$$$$$&##$!
!!o$&&&&&&&$$$$$$$$$$$$$$$$&&#&*!
!!ooo! !!o$&&#&&&&&&&&$$$$$$$$$$$$$$&&##&*!
!!oooo*$*o !oo! !!o*$&&###&&$$&&&&&&$$$$$$$$$$$$$$$&##&$*!
!!oooo**$*o !o****! !o*$&&&#######&$$$&&$$$$$$$$$$$$$$$$$$&##&&&*!
!oooo**$$$$$$o! !o****o! o$&#######&&###&$$$$&$$$$$$$$$$$$$$$$$&&&###&&*o
!oo*$$&&&&$$&&&$oo*$$$*o! !*&##&####&&&###&$$$$$$$$$$$$$$$$$$$$$$$$&&&#####&*o
!oooo$&&&&&#&&&#&&$$$$*! !o$&######&&#&&##&$$$$$$$$&&&&&&&&&&$$$$$$$&&&#&&####&$!
**$$$&&&&###&&&&$$$$*! o$&##########&&###$*$$$$&&&&&&&&&&&$$$$$$$$&&#&&#######&$*!
o$##&&&&&&&#&&&$$$$*! o*&###&#######&&&##&$**$$$$$&&&&&$$$$$$$$$$$$&&#######&&&##&$o
*&&&&&&&&&$$$$$$&$! !*&&&################$$**$$$$$$$$$$$$$$$$$$$$$&&############&##&*!
o$&$$$$$$$$$$$&&$o o$&###################$$$$$$$$$********$**$*$$&&###########&&&&##&$*o
!$$$$$$$$$&&&&&o o$&#&##################$$$$$$$$$$$$$$$$$$$$$$$$&##############&&&####&o!!!
!$$$$$$$$&&&##&! o$##&&##################$$$$*$$$$$$$$$$$$$$$$$$&&################&&&&##&$*!
!$&#&&&#######&o *&&######################$$$$**$$$$$$$$$$$$$$$$$&#################&#&&&###&$o
$############&o! o&##&###&################&$$*$$********$$$$$$$$$&&#################&#&&&&###&$o
$############&*! o$#######&&###############&$$$$**$$$****$$$$$$$$&&&#####################&&&##&#&o!
!$#############&*! o$#########################&$$$$$$$$$**$$$$$$$$$$&&#######&&##############&&&####&*
!&##############&* !*&&#########################&$$$$$$$$$$*$$$$$$$$$&&########&&###############&&###&&$!
!*&###############&o o$&###########################&$**$$$$$$$*$$$$$$$$$&&&###############################&o
o$#################o! !*&#############################&$$*$$$$$$$$$$$$$$$$$&&#######&&&######################&*
o*&################*! !$&##############################&$$$$$$$$$$$$$$$$$$$&&#########&&&######################$!
o*&################&*! !$&&##############################&$$$$$$$$$$$$$$$$$$$&###################################&o!
! !!o*##################&*o*####################&&&##########&$$$$$$$$$$$$$$$$$$&&###################################&*o
!!!o*##################&$*$#################################&$$$$$$$$$$$$$$$$$&&&####################################$*!!!!!!
!!!!!o*###################&&&#################################&$$$$$$$$$$$$$$$$$&&&####################################&*!!!!!!
!!!!!!o*#######################################################&$$$$$$$$$$$$$$$$&&#######################################$o!!!!!
!!!!!!o*&######################################################&$$$$$$$$$$$$$$$$&&#######################################&o!!!!!
!!!!!!!o&######################################################&$$$$$$$$$$$$$$&&#########################################&*!!!!!
!!!!!!!!$#####################################################&&$$$$$$$$$$$$$&###########################################&*o!!!!
!!!!!!!!$#######################################&&&&##########&&$$$$$$$$$$$$&&###########################################&*o!!!!
!!!!!!!!*&####################################&$****$&&&######&&$$$$$$$$$$$&&############################################&*o!!!!
!!!!!!!!o&###################################&$$$$$*****$$&###&&$$$$$$$$$$$&#############################################&*o!!!!
!!!!!!!!o$#################################&&$$*$&&$$$**oo*&&&&$$$$$$$$$$$&##############################################&$o!!!!
!!!!!!o!o$&####&&&&#########################&$*oo**********$&&&$$$$$$$$$$$&###############################################$o!!!!
!!!!!!oo**$&&&&&&&&&###&&###################&&$**ooooooo**$$$$&$$$$$$$$$$&&###############################################&o!!!!
oooo!o***$$&&&&&&&&&&&&&&&&&&&&&&&&&&&&&###&&&#&$$***oooo***$$$$$$$$$$$$$&################################################&o!!!!
oo****$$$&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&$$****$$***$$$$$$$$$$&&################################################&o!!!!
**$$$&&&&&&&&&&&#&&&&&&&&&&&&&&&&&&&&&&&&&&&&$$$$$$*****$$$$$$$*$$$$$$$$&&################################################&*o!!!
$$$&#&&&##&&&&&###&&&&&&&&&&&&&&&&&&&&&&&&&&&&$$$****o**$$$$$$*****$$$$$$#################################################&*o!!!
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
typedef struct
{
unsigned int count[2];
unsigned int state[4];
unsigned char buffer[64];
}MD5_CTX;
#define F(x,y,z) ((x & y) | (~x & z))
#define G(x,y,z) ((x & z) | (y & ~z))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y ^ (x | ~z))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
#define FF(a,b,c,d,x,s,ac) \
{ \
a += F(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define GG(a,b,c,d,x,s,ac) \
{ \
a += G(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define HH(a,b,c,d,x,s,ac) \
{ \
a += H(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
#define II(a,b,c,d,x,s,ac) \
{ \
a += I(b,c,d) + x + ac; \
a = ROTATE_LEFT(a,s); \
a += b; \
}
void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);
void MD5Final(MD5_CTX *context,unsigned char digest[16]);
void MD5Transform(unsigned int state[4],unsigned char block[64]);
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len);
unsigned char PADDING[]={0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void MD5Init(MD5_CTX *context)
{
context->count[0] = 0;
context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)
{
unsigned int i = 0,index = 0,partlen = 0;
index = (context->count[0] >> 3) & 0x3F;
partlen = 64 - index;
context->count[0] += inputlen << 3;
if(context->count[0] < (inputlen << 3))
context->count[1]++;
context->count[1] += inputlen >> 29;
if(inputlen >= partlen)
{
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i = partlen;i+64 <= inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index = 0;
}
else
{
i = 0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}
void MD5Final(MD5_CTX *context,unsigned char digest[16])
{
unsigned int index = 0,padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
padlen = (index < 56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}
void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+1] = (input[i] >> 8) & 0xFF;
output[j+2] = (input[i] >> 16) & 0xFF;
output[j+3] = (input[i] >> 24) & 0xFF;
i++;
j+=4;
}
}
void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[i] = (input[j]) |
(input[j+1] << 8) |
(input[j+2] << 16) |
(input[j+3] << 24);
i++;
j+=4;
}
}
void MD5Transform(unsigned int state[4],unsigned char block[64])
{
unsigned int a = state[0];
unsigned int b = state[1];
unsigned int c = state[2];
unsigned int d = state[3];
unsigned int x[64];
MD5Decode(x,block,64);
FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
unsigned char s[20], e[20], t[20];
int dfs(int x, int l) {
{
MD5_CTX md5;
MD5Init(&md5);
MD5Update(&md5, s, strlen((char *)s));
MD5Final(&md5, e);
if (strcmp((char *)e, (char *)t) == 0) {
return 1;
}
}
if (x == l) {
return 0;
}
for (s[x] = 32; s[x] <= 126; s[x]++) { // ALL VISIBLE CHARS
if (dfs(x + 1, l)) {
return 1;
}
}
s[x] = 0;
return 0;
}
void cases(int length) {
int i, tmp;
for (i = 0; i < 16; i++) {
scanf("%2x", &tmp);
t[i] = tmp;
}
dfs(0, length);
printf("%s\n", s);
}
// You need not to understand the procedure of MD5.
// 浣犱笉闇€瑕佺悊瑙D5銆?
// MD5 is a string hash function.
// MD5鏄竴涓瓧绗︿覆hash鍑芥暟銆?
// You need to answer the following 10 questions, by the way, you know the md5 of each answer.
// 浣犻渶瑕佸仛鐨勬槸鍥炵瓟涓嬮潰10涓棶棰橈紝浣犵煡閬撲粬浠瓟妗堢殑MD5銆?
// The pascal code can be a reference to you.
// 浣犲彲浠ュ弬鑰僷ascal浠g爜銆?
int main(int argc, char *argv[])
{
int i;
unsigned char example[] = "ab";
MD5_CTX md5;
MD5Init(&md5);
MD5Update(&md5, example, strlen((char *)example));
MD5Final(&md5, e);
for (i = 0; i < 16; i++) {
// printf("%02x", e[i]); // 187ef4436122d1cc2f40dc2b92f0eba0
}
// printf("\n");
cases(11);
// When did the first NOI hold? 4 digits.
// 绗竴骞碞OI鏄摢骞达紵4浣嶆暟瀛椼€?
cases(11);
// In this line, the answer is the most common 6-char password.
// 鍦ㄨ繖涓€琛岋紝绛旀鏄渶甯歌6浣嶇殑瀵嗙爜銆?
cases(11);
// In this line, you need to print the name of him, whose photo is in the begin of this file.
// 鍦ㄨ繖涓€琛屼綘闇€瑕佽緭鍑轰竴涓汉鍚嶏紝浠栫殑鐓х墖鍦ㄨ繖涓枃浠剁殑寮€澶淬€?
// He also gives a class during WC but he is absent from this contest.
// 浠栨潵杩欐鍐护钀ヨ璇句簡锛屼絾鏄粬娌℃湁鏉ヨ繖鍦鸿€冭瘯銆?
// Use the lowercase of pinyin, without any white space. e.g. chenxumin
// 鐢ㄥ皬鍐欐嫾闊筹紝涓嶅姞浠讳綍绌虹櫧銆?姣斿 chenxumin
cases(11);
// In this line, the answer is only three visible symbols.
// 鍦ㄨ繖涓€琛岋紝绛旀鏄?涓彲瑙佺鍙枫€?
cases(11);
cases(11);
cases(11);
cases(11);
// In each of these 4 lines, the answer is a single word.
// 鍦ㄨ繖4琛岋紝绛旀鏄竴涓崟璇嶃€?
cases(7);
// In this line, the answer is two words joined with a space char ' '.
// 鍦ㄨ繖涓€琛岋紝绛旀鏄袱涓崟璇嶏紝鐢ㄧ┖鏍煎垎闅斻€?
cases(11);
// In this line, the answer is a long word with '-', to make it easier, I delete the '-'.
// The last 6 answers form a famous sentence.
// 鍦ㄨ繖涓€琛岋紝绛旀鏄竴涓敱'-'杩炴帴鐨勯暱鍗曡瘝锛屼负浜嗚杩欎釜闂鏇寸畝鍗曪紝鎴戝垹鎺変簡'-'銆?
// 鏈€鍚?涓缁勬垚浜嗕竴鍙ュ悕瑷€銆?
return 0;
}乱码抱歉(DEV无法识别UTF,只识别GBK)
怎么是个笔试题,还有MD5。
第一个和第二个无疑问,第三个查一下当年资料,第四个请暴力枚举。
后面六个连在一起?其实是独立宣言中的“我们相信真理是不证自明的”(We hold these truths to be self-evident),对应填词即可,这句话写在program10里了。
Level 10
woc这800KB的程序把浏览器卡崩了。
发现所有的函数其实都是若干个_()函数的叠加,把所有的函数都化开就好了。
出题人这么喜欢独立宣言你倒是别留在这地方出这些神秘题了。
诶?RE了,原来这神经DEV递归坏了。
好吧只能使用统计大法了。
统计所有字串调用中每个字符出现多少次。