教你Hack

· · 个人记录

\text{Part~1~What~is~hack}

Hack 一般是指通过特殊数据是一些错误解法挂断(WA,TLE,MLE)这篇文章主要讲一些hack的方式

\text{Part~2~How~to~hack} \text{Part~2.1~记搜看初值}

P1880

目标代码

Zhaoyuhang2008很不友好,一上来就要叉题解

记搜看初值,所以我们来到说明/提示:

Hack Data 生成器: ```cpp #include<bits/stdc++.h> using namespace std; int main() { freopen("HackData1.in","w",stdout); cout<<100<<endl; for(int i=0;i<100;i++) { cout<<0<<" "; } cout<<endl; return 0; } ``` $\text{Part~2.2~不开ll见祖宗}

这已经众人皆知了吧,也很好hack.。不放例题了

\text{Part~2.3~INF看范围}

P6190

目标代码↓

//用的OMG_wc的题解稍加改动
//STO luogu.com.cn/blog/wcsb/solution-p6190 OTZ
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = (1<<30);
const int N = 100;
typedef LL Matrix[N][N];
int sz;
LL a[N][N];
void matrix_mul(Matrix A, Matrix B, Matrix res) {
    Matrix C;
    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < sz; j++) {
            C[i][j] = INF;
            for (int k = 0; k < sz; k++) {
                C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
            }
        }
    }
    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < sz; j++) {
            res[i][j] = C[i][j];
        }
    }
}
void matrix_pow(Matrix A, LL p, Matrix res) {
    Matrix x;
    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < sz; j++) {
            x[i][j] = A[i][j];
            res[i][j] = a[i][j];
        }
    }
    while (p) {
        if (p & 1) matrix_mul(res, x, res);
        p >>= 1;
        matrix_mul(x, x, x);
    }
}


struct Edge {
    int u, v, w;
} b[2505];
int main() {
    int n, m, K;
    scanf("%d%d%d", &n, &m, &K);
    sz = n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            a[i][j] = i == j ? 0 : INF;
    for (int i = 0; i < m; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        u--;
        v--;
        b[i] = {u, v, w};
        a[u][v] = min(a[u][v], (LL)w);
    }
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
    }
    Matrix c;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            c[i][j] = a[i][j];
            for (int k = 0; k < m; k++) {
                c[i][j] = min(c[i][j], a[i][b[k].u] - b[k].w + a[b[k].v][j]);
            }
        }
    }
    matrix_pow(c, K, c);
    printf("%lld\n", c[0][n - 1]);
    return 0;
}

许多人喜欢用 2^{30} 做INF,但分析以下这题数据范围,最坏情况 k=0,m=2500,t_i=10^9 就会直接爆 int 固 INF 要取 2^{60}

Hack Data 生成器:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("HackData2.in","w",stdout);
    cout<<100<<" "<<250<<" "<<0<<endl;
    for(int i=1;i<100;i++)
    {
        cout<<i<<" "<<i+1<<int(1e9)<<endl;
    }
    for(int i=1;i<=250-99;i++)
    {
        cout<<rand()%100+1<<" "<<rand()%100+1<<int(1e9)-(rand()%10)<<endl;
    }
    cout<<endl;
    return 0;
}
\text{Part~2.4~深搜死循环}

P1032

目标代码↓

#include<bits/stdc++.h>
using namespace std;
string a,b,w[10],m[10];
int n,ans=1e8;
bool ok;
map<string,bool>mp;
void dfs(int d)
{
    if(mp[a]) return;
    if(a==b)
    {
        ans=min(ans,d);
        ok=1;
        return; 
    }
    mp[a]=1;
    if(d>10) return;
    for(int i=0;i<n;i++)
    {
        int c=a.find(w[i]);
        while(c<a.length()&&c>=0)
        {
            string temp=a;
            a.replace(c,w[i].length(),m[i]);
            dfs(d+1);
            a=temp;
            c=a.find(w[i],c+1);
        }
    }

}
int main()
{
    cin>>a>>b;
    while(cin>>w[n]>>m[n])n++;  
    mp.clear();
    dfs(0);
    if(ok&&ans<=10&&ans!=0)
    {
        cout<<ans;
        return 0;
    }
    else
    {
        cout<<"NO ANSWER!";
        return 0;
    }
    return 0;
}
}

这题DFS容易走到死循环:

现有两个字符串 a 和 b,a 可以变成 b 。初始字符串能变成 a,然而 a 又是 b 字串,就会变成这个样子:

初始字符串变成 a;

$b$ 的 $a$ 子串变成 $b$; $b$ 刚变成 $b$ 的那部分的 $a$ 子串变成 $b$; $b$ 刚变成 $b$ 的那部分刚变成 $b$ 的 $a$ 子串变成 $b$; ………… ~~禁止套娃~~ Hack Data: ``` a aaaaa a a112233445566778899 778899 a 112233 a 445 a 566 a 55 a ``` $\text{Part~2.5~广搜重状态}

P1032

目标代码↓

//时律的题解稍加改动
//STO https://www.luogu.com.cn/blog/Dimly/solution-p1032 OTZ
#include<bits/stdc++.h>
using namespace std;
struct data{
    string a;
    int ans;
};
queue<data> q;
string x[15],y[15];
int main()
{
    int u=1;
    string a,b;
    cin>>a>>b;
    while(cin>>x[u]>>y[u])
        u++;
    u--;
    q.push(data{a,0});
    while(!q.empty())
    {
        data s=q.front();
        if(s.ans>10)
            break;
        if(s.a==b)
        {
            printf("%d",s.ans);
            return 0;
        }
        for(int i=1,v=0;i<=u;i++,v=0)
            while(s.a.find(x[i],v)!=-1)
            {
                string w=s.a;
                int m=s.a.find(x[i],v);
                w.replace(m,x[i].size(),y[i]);
                q.push(data{w,s.ans+1});
                v=m+x[i].size();
            }
        q.pop();
    }
    printf("NO ANSWER!");
}

广搜的弱点就是出队一个,进队一堆,会有重复状态,所以就对重复状态去Hack,就行了:

Hack Data:

abaaaba abcdaba
a b
b d
d e
e f
f g
g c
\text{Part~2.6~臭名昭著的Hash}

hash最好别再CF用,您会被万种方法hack

https://www.cnblogs.com/Hs-black/p/12219270.html

全学会了再来写。

\text{Part~2.7~抓住重点,hack特点}

文本编辑器

目标代码↓

#include <bits/stdc++.h> 
using namespace std;

const int MAXN = 500005;
char a[MAXN], op[2],  ch[2];
int n, pos, end;

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%s", op);
        switch(op[0]) {
            case 'f':
                if(pos < end)
                    pos++;
                break;

            case 'b':
                if(pos > 0)
                    pos--;
                break;

            case 'd':
                for(int j = pos; j < end - 1; j++)
                    a[j] = a[j+1];
                if(end > pos)
                    end--;  
                break;

            case 'i':
                scanf("%s", ch);
                for(int j = end - 1; j >= pos; j--)
                    a[j+1] = a[j];
                a[pos] = ch[0];
                pos++;
                end++;
                break;
        }
    }

    for(int i = 0; i < end; i++)
        printf("%c", a[i]);
    printf("\n");
    return 0;
}

//By lianix

数组的特点是插入/删除慢,所以把所有操作都弄成插入/删除。

Hack Data 生成器:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("HackData3.in","w",stdout);
    cout<<500000<<endl;
    for(int i=1;i<=200000;i++)
    {
        cout<<'i'<<" "<<char(rand()%26+'A')<<endl;
    }
    for(int i=1;i<=200000;i++)
    {
        if(rand()%10==0)cout<<'i'<<" "<<char(rand()%26+'A')<<endl;
        else cout<<'f'<<endl;
    }
    for(int i=1;i<=100000;i++)
    {
        cout<<'i'<<" "<<char(rand()%26+'A')<<endl;
    }
    return 0;
}

P5057

目标代码↓

#include<bits/stdc++.h>
using namespace std;
int m,n,a[100008];
int main()
{
    cin>>n>>m;
    while(m--)
    {
        short opt;
        cin>>opt;
        if(opt==1)
        {
            int l,r;
            cin>>l>>r;
            for(int i=l;i<=r;i++)a[i]^=1;
        }
        else
        {
            int idx;
            cin>>idx;
            cout<<a[idx]<<endl;
        }
    }
    return 0;
}

暴力的修改是比BIT慢的,但查询更快,所以全是修改就行

Hack Data 生成器:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    freopen("HackData4.in","w",stdout);
    cout<<100000<<" "<<500000<<endl;
    for(int i=1;i<=500000;i++)
    {
        cout<<1<<" "<<1<<" "<<100000<<endl;
    }
    return 0;
}
\text{Part~2.8~珂爱的珂朵莉}

因为珂朵莉很可爱,所以让我们来hack ODT吧!(大雾)

P5066(藏了,是人人本着正义之名)

ODT之所以快,就是因为有推平操作,所以没有推平操作ODT就会退化成暴力。

\text{Part~3~会Hack有什么用}
  1. 起码可以让你CF上分

  2. 出题时可以轻易叉掉错误算法

  3. 叉题解可以让您看起来很巨

\text{That~is~all.Thanks~for~reading!~}