这题不难,我写的一个复杂度 $ O(n^2) $ 的递归算法。。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < b; i++) #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define index(a) (a - 'A') #define transUpp(a) (a - 32) #define transLow(a) (a + 32) #define ll long long #define PB push_back int gcd(int a, int b){return b == 0 ? a : gcd(a%b, a);} const int N = 782; int num[N] = { 6,2,5,5,4,5,6,3,7,6 }, has[N][N]; int counts = 0; void re(int n, int l1, int l2) { if (l1 + l2 < N) { has[l1][l2] = 1; if (n == num[l1] + num[l2] + num[l1 + l2]) counts++; if (!has[l1][l2 + 1]) re(n, l1, l2 + 1); if (!has[l1 + 1][l2]) re(n, l1 + 1, l2); } } int main() { // rep(i, 0, N) rep(j, 0, N) has[i][j] = 0; // 去掉这行初始化,投机优化一点速度 for (int k = 10; k < N; k++) num[k] = num[k%10] + num[k/10]; int n; cin >> n; re(n - 4, 0, 0); cout << counts << endl; return 0; }