Project Euler

· · 个人记录

代码上传在这个 Github 仓库:https://github.com/EasonOIer/Project-Euler-Solution/wiki

001 ~ 100

Problem 1: Multiples of 3 or 5 / 3 或 5 的倍数

直接枚举即可。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int sum = 0; for(int i = 1;i < 1000;i++) {
        if(i % 3 == 0 || i % 5 == 0) sum += i;
    } cout << sum << endl;
    return 0;
}

注意是 <1000,不是 \le1000。

Problem 2: Even Fibonacci numbers / 偶斐波那契数

同样枚举即可。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a = 1, b = 1, c = 0, sum = 0;
    while(c <= 4000000) {
        c = a + b; a = b; b = c; 
        if(c % 2 == 0) sum += c;
    } cout << sum << endl;
    return 0;
}

Problem 3: Largest prime factor / 最大质因数

可以直接 O(\sqrt n) 枚举因子。注意 num 最后可能是个大质数。

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long num = 600851475143; long long ans = 0;
    for(long long i = 2;i * i <= num;i++) {
        if(num % i == 0) {
            num /= i, ans = max(ans, i);
        } 
    } cout << max(ans, num) << endl;
    return 0;
}

Problem 4: Largest palindrome product / 最大回文乘积

直接枚举两个数。可以使用 to_string 函数将数转成字符串。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int ans = 0; 
    for(int i = 100;i <= 999;i++) {
        for(int j = 100;j <= 999;j++) {
            string s = to_string(i * j);
            string t = s; reverse(t.begin(), t.end()); 
            if(s == t) ans = max(ans, i * j);
        }
    } cout << ans << endl;
    return 0;
}

Problem 5: Smallest multiple / 最小公倍数

就是求 \displaystyle\operatorname{lcm}\limits_{i=1}^{20}i。那么可以使用 C++ 自带的(从 C++17 开始支持)gcd 函数。

#include <bits/stdc++.h>
using namespace std;
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main() {
    long long ans = 1; for(int i = 1;i <= 20;i++) ans = lcm(ans, i);
    cout << ans << endl;
    return 0;
}

Problem 6: Sum square difference / 平方和与和平方之差

枚举。注意不一定第一个比第二个大,要取绝对值。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int sum1 = 0, sum2 = 0;
    for(int i = 1;i <= 100;i++) sum1 += i * i, sum2 += i;
    cout << abs(sum1 - sum2 * sum2) << endl;
    return 0;
}

Problem 7: 10001st prime / 第 10001 个质数

猜一波小的质数不会太大,于是直接枚举。

#include <bits/stdc++.h>
using namespace std;
bool isprime(int x) {
    for(int i = 2;i * i <= x;i++) {
        if(x % i == 0) return false;
    } return true;
} int main() {
    int cnt = 0; for(int i = 2;;i++)
        if(isprime(i) && ++cnt == 10001) return cout << i, 0; 
    return 0;
}

Problem 8: Largest product in a series / 连续数字最大乘积

直接枚举。

#include <bits/stdc++.h>
using namespace std;
string s = " 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
int main() {
    long long mx = 0;
    for(int i = 1;i <= 988;i++) {
        long long p = 1; for(int j = i;j <= i+12;j++) p *= (s[j] - '0');
        mx = max(mx, p);
    } cout << mx << endl;
    return 0;
}

Problem 9: Special Pythagorean triplet / 特殊毕达哥拉斯三元组

可以枚举 a,b,然后算出 c 后再判断。

#include <bits/stdc++.h>
using namespace std;
int main() {
    for(int a = 1;a <= 1000;a++) {
        for(int b = 1;b <= 1000;b++) {
            if((a + b) >= 1000) break;
            if(a * a + b * b == (1000 - a - b) * (1000 - a - b)) {
                cout << a * b * (1000 - a - b) << endl; 
            }
        }
    }
    return 0;
}

Problem 10: Summation of primes / 质数求和

看起来如果 n=2000000 时,O(n\sqrt n) 会超时,但是实际上远远跑不满,所以直接枚举。记得开 long long。

#include <bits/stdc++.h>
using namespace std;
int isp(int x) {
    for(int i = 2;i * i <= x;i++) {
        if(x % i == 0) return false;
    } return true;
} int main() { long long ans = 0;
    for(int i = 2;i <= 2000000;i++) {
        if(isp(i)) ans += i;
    } cout << ans << endl;
    return 0;
}

Problem 11: Largest product in a grid / 方阵中的最大乘积

这个方阵不太方便直接弄到程序里,所以当做输入数据就可以了。

#include<iostream>
using namespace std;
int num[30][30], ans;
int dirx[4] = {-1, 0, 1, 1};
int diry[4] = {1, 1, 1, 0};
int main() {
    for (int i = 5; i < 25; i++) {
        for (int j = 5; j < 25; j++) {
            cin >> num[i][j];
        }
    }
    for (int i = 5; i < 25; i++) {
        for (int j = 5; j < 25; j++) {
            for (int k = 0; k < 4; k++) {
                int t = num[i][j];
                for (int l = 1; l < 4; l++) {
                    int x = i + dirx[k] * l;
                    int y = j + diry[k] * l;
                    t *= num[x][y];
                }
                ans = max(ans , t);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Problem 12: Highly divisible triangular number / 有很多约数的三角形数

直接枚举直接枚举直接枚举。怎么前面的题都是无脑题啊?

#include <bits/stdc++.h>
using namespace std;
bool fac(long long x) {
    int num = 0; for(long long i = 1;i * i <= x;i++) {
        if(x % i == 0) num += 2;
        if(i * i == x) num--; 
    } return num >= 500;
} int main() {
    long long ans = 1; while(!fac((ans + 1) * ans / 2)) ans++;
    cout << (ans + 1) * ans / 2 << endl;
    return 0;
}

Problem 13: Large sum / 大和

因为只需要求前 10 位,所以我们把每个数前十五位存到 long long 里加起来即可。

#include <bits/stdc++.h>
using namespace std;
#define int long long
string s[105]; int x[105];
signed main() {
    int sum = 0;
    for(int i = 1;i <= 100;i++) {
        cin >> s[i];
        for(int j = 0;j < 15;j++)
            x[i] = x[i] * 10ll + (s[i][j] - '0');
        sum += x[i];
    } cout << to_string(sum).substr(0, 10) << endl;
    return 0;
}

Problem 14: Longest Collatz Sequence / 最长考拉兹序列

直接暴力枚举。注意要开 long long,因为大概在 1.13 \times 10^5 的地方有一个数运算的时候会爆 int。

#include <bits/stdc++.h>
using namespace std;
#define int long long
int mx, mxi;
signed main() {
    for(int i = 1;i <= 1000000;i++) {
        int cnt = 0, now = i;
        while(now != 1)
            (now % 2 ? now = 3 * now + 1 : now >>= 1), cnt++;
        if(cnt > mx) mx = cnt, mxi = i;
    } cout << mxi << endl;
    return 0;
}

Problem 15: Lattice Paths / 网格路径

小学数学标数法。记得 long long。

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[25][25];
signed main() {
    a[1][1] = 1;
    for(int i = 1;i <= 21;i++)
        for(int j = (i == 1 ? 2 : 1);j <= 21;j++)
            a[i][j] = a[i-1][j] + a[i][j-1];
    cout << a[21][21] << endl;
    return 0;
}

Problem 16: Power Digit Sum / 幂的数字和

写个高精度处理一下就行。代码过长,用折叠块放。

:::info[代码]

#include <bits/stdc++.h>
using namespace std;
/*Bigint Template*/
using LL = long long;
const int N = 10005;

struct Int {
    int len, a[N];

    Int() {
        memset(a, 0, sizeof a);
        len = 1;
    }

    Int& operator = (const string& s) {
        len = s.length();
        for (int i = 0; i < s.length(); ++i)
            a[len - i] = s[i] - '0';
        return *this;
    }

    Int& operator = (LL x) {
        return *this = to_string(x);
    }
};

Int operator + (const Int& A, const Int& B) {
    Int C;
    C.len = max(A.len, B.len);
    int sum = 0;
    for (int i = 1; i <= C.len; ++i) {
        if (i <= A.len)
            sum += A.a[i];
        if (i <= B.len)
            sum += B.a[i];
        C.a[i] = sum % 10;
        sum /= 10;
    }
    if (sum)
        C.a[++C.len] = sum;
    return C;
}

bool operator < (const Int& A, const Int& B) {
    if (A.len != B.len)
        return A.len < B.len;
    for (int i = A.len; i >= 1; --i)
        if (A.a[i] != B.a[i])
            return A.a[i] < B.a[i];
    return false;
}

Int operator - (const Int& A, const Int& B) {
    Int C;
    C.len = max(A.len, B.len);
    int d = 0;
    for (int i = 1; i <= C.len; ++i) {
        d += A.a[i];
        if (i <= B.len)
            d -= B.a[i];
        if (d < 0) {
            C.a[i] = d + 10;
            d = -1;
        } else {
            C.a[i] = d;
            d = 0;
        }
    }
    while (C.len > 1 && !C.a[C.len])
        --C.len;
    return C;
}

Int operator * (const Int& A, int b) {
    Int C;
    C.len = A.len;
    if (!b)
        return C = "0";
    LL mul = 0;
    for (int i = 1; i <= C.len; ++i) {
        mul += 1ll * A.a[i] * b;
        C.a[i] = mul % 10;
        mul /= 10;
    }
    while (mul) {
        C.a[++C.len] = mul % 10;
        mul /= 10; 
    }
    return C;
}

Int operator / (const Int& A, int b) {
    Int C;
    C.len = A.len;
    LL r = 0;
    for (int i = C.len; i >= 1; --i) {
        r = r * 10 + A.a[i];
        C.a[i] = r / b;
        r %= b;
    }
    while (C.len > 1 && !C.a[C.len])
        --C.len;
    return C;
}
Int operator * (const Int& A,const Int& B) {
    Int c; c = 0;
    for(int i = 1;i <= B.len;i++) {
        for(int j = 1;j <= A.len;j++) {
            c.a[i+j-1] += A.a[j] * B.a[i];
        }
    }
    for(int i = 1;i < A.len+B.len;i++) {
        if(c.a[i] > 9) {
            c.a[i+1] += c.a[i] / 10;
            c.a[i] %= 10;
        }
    }
    c.len = A.len + B.len;
    while(c.a[c.len] == 0 && c.len > 1) c.len--;
    return c;
}
bool operator == (const Int& A,const Int& B) {
    if(A.len != B.len) return false;
    for(int i = 1;i <= A.len;i++) {
        if(A.a[i] != B.a[i]) return false;
    }
    return true;
}
bool operator <= (const Int& A,const Int& B) {
    return (A < B || A == B);
}
bool operator > (const Int& A,const Int& B) {
    return !(A <= B);
}
bool operator >= (const Int& A,const Int& B) {
    return !(A < B);
}
istream& operator >> (istream &in, Int& x) {
    string s;
    in >> s;
    x = s;
    return in;
}

ostream& operator << (ostream &out, const Int& x) {
    for (int i = x.len; i >= 1; --i)
        out << x.a[i];
    return out;
}
/*Bigint Template*/

int main() {
    Int a; a = "1"; Int b; b = "2";
    for(int i = 1;i <= 1000;i++) {
        a = a * b;
    } int sum = 0;
    for(int i = 0;i <= 1000;i++)
        sum += a.a[i];
    cout << sum << endl;
    return 0;
}

:::

101 ~ 200

前面题有点水,从 101 开始好了。

Problem 101: Optimum polynomial / 最优多项式

上难度。考虑先将 u_1 \sim u_{10} 算出来,然后直接对着每个 u_1 \sim u_i 拉插一下就可以得到题目中所说的“最优多项式” 了。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 101;
int x[N], y[N];
int quick_pow(int sum,int k) {
    int ret = 1; while(k) {
        if(k & 1) ret = ret * sum; k >>= 1; sum = sum * sum;
    } return ret;
} int Lagerange(int k,int sum) {
    int ret = 0;
    for(int i = 1;i <= k;i++) {
        int now = y[i];
        int sum1 = 1, sum2 = 1;
        for(int j = 1;j <= k;j++) {
            if(i == j)continue;
            sum1 *= sum - x[j];
            sum2 *= x[i] - x[j];
        }
        ret += now * sum1 / sum2;
    }
    return ret;
} signed main() {
    for(int i = 1;i <= 11;i++) {
        x[i] = i; int cur = 1; y[i] = 1;
        for(int j = 1;j <= 10;j++) {
            cur = -cur;
            y[i] += cur * quick_pow(x[i],j);
        }
    } int ans = 1; for(int i = 2;i <= 10;i++) {
        ans += Lagerange(i,i+1);
    } cout << ans << endl; return 0;
}