P1149 [NOIP 2008 提高组] 火柴棒等式

题目地址: https://www.luogu.com.cn/problem/P1149

Solution:

分析:一道暴力枚举题,先枚举出 AB 所有可能的数字,设$f_{i}$ 表示拼成数字 i 所用的火柴数,若$f_{A}+f_{B}+f_{A+B}=n−4$,答案加一,这里减 4 是减去加号和等号所用火柴。

因为n的范围是n(1≤n≤24),因此枚举总量不会太大,大致枚举到1000就可以了。

AC Code:

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

int a[]={6, 2, 5, 5, 4, 5, 6, 3, 7, 6},n,ans;
int f(int x){
    if(x<10)
        return a[x];
    return f(x/10)+a[x%10];
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<=1000;++i)
        for(int j=0;j<=1000;++j)
            ans+=(f(i)+f(j)+f(i+j)==n-4);
    printf("%d\n",ans);
    return 0;
}

P1149 [NOIP 2008 提高组] 火柴棒等式
https://zer0ptr.github.io/2025/07/12/P1149-Matchstick/
Author
zer0ptr
Posted on
July 12, 2025
Licensed under