跳到正文

题解

题解:P12174 [蓝桥杯 2025 省 Python B] A · B Problem

_initializer_list_约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年8月19日
收录
2026年8月19日
更新
2026年11月15日
篇幅
约 10 分钟
获赞
1
题目
P12174

版本

  1. v1当前

题目传送门

思路

题目中内积为:

XA⋅XB+YA⋅YBX_A \cdot X_B+Y_A \cdot Y_B

设 x=XA⋅XB, y=YA⋅YBx=X_A\cdot X_B,\ y=Y_A\cdot Y_B,那么条件变成 x+y≤Lx+y \leq L,其中 x,yx,y 均为正整数。

对于 xx 的每个因子 dd,可以让 XA=d, XB=xdX_A=d, \ X_B=\dfrac{x}{d},因此总的方案数为 xx 的因子个数。

设 ii 有 d(i)d(i) 个因子,由于 xx 有 d(x)d(x) 种方案,yy 有 d(y)d(y) 种方案,满足 x+y≤Lx+y\leq L,答案为:

∑x=1L−1∑y=1L−xd(x)⋅d(y)\sum_{x=1}^{L-1}\sum_{y=1}^{L-x}d(x) \cdot d(y)

定义前缀和 prei=∑k=1id(k)pre_i=\sum_{k=1}^{i}d(k),那么上式可以简化为:

∑x=1L−1d(x)⋅preL−x\sum_{x=1}^{L-1}d(x)\cdot pre_{L-x}

代码

AC Code
C++
// Problem: P12174 [蓝桥杯 2025 省 Python B] A · B Problem
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P12174
// Memory Limit: 512 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// Code by wangxy0311

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

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
template <class T>
T randint(T l, T r) {
    uniform_int_distribution<T> dist(l, r);
    return dist(rnd);
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef __int128 i128;
typedef unsigned __int128 u128;
typedef long double ld;

#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fileIO(s)               \
  freopen(s ".in", "r", stdin); \
  freopen(s ".out", "w", stdout);
#define endl '\n'
#define all(x) begin(x), end(x)

inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename T>
void chkmax(T &a, T b) { if (b > a) a = b; }
template <typename T>
void chkmin(T &a, T b) { if (b < a) a = b; }

template <int MOD>
struct Modint {
    ll val;
    Modint(ll v = 0) { val = (v % MOD + MOD) % MOD; }
    Modint operator+(const Modint & x) const { return Modint(val + x.val); }
    Modint operator-(const Modint & x) const { return Modint(val - x.val); }
    Modint operator*(const Modint & x) const { return Modint((i128)val * x.val % MOD); }
    Modint operator^(ll b) const { Modint res = 1, a = *this; for (; b; b >>= 1, a *= a) if (b & 1) res *= a; return res; }
    Modint operator^(const Modint & b) const { return *this ^ b.val; }
    Modint operator~() const { return *this ^ MOD - 2; }
    Modint operator/(const Modint & x) const { return *this * ~x; }
    Modint operator-() const { return Modint(-val); }
    Modint & operator+=(const Modint & x) { val = (val + x.val) % MOD; return *this; }
    Modint & operator-=(const Modint & x) { val = (val - x.val + MOD) % MOD; return *this; }
    Modint & operator*=(const Modint & x) { val = (i128)val * x.val % MOD; return *this; }
    Modint & operator^=(ll b) { return *this = *this ^ b; }
    Modint & operator^=(const Modint & b) { return *this = *this ^ b.val; }
    Modint & operator/=(const Modint & x) { return *this = *this / x; }
    Modint & operator++() { return *this += Modint(1); }
    Modint operator++(int) { Modint tmp = *this; ++*this; return tmp; }
    Modint & operator--() { return *this -= Modint(1); }
    Modint operator--(int) { Modint tmp = *this; --*this; return tmp; }
    bool operator==(const Modint & x) const { return val == x.val; }
    bool operator!=(const Modint & x) const { return val != x.val; }
    bool operator<(const Modint & x) const { return val < x.val; }
    bool operator>(const Modint & x) const { return val > x.val; }
    bool operator<=(const Modint & x) const { return val <= x.val; }
    bool operator>=(const Modint & x) const { return val >= x.val; }
    friend Modint operator+(ll v, const Modint & x) { return Modint(v) + x; }
    friend Modint operator-(ll v, const Modint & x) { return Modint(v) - x; }
    friend Modint operator*(ll v, const Modint & x) { return Modint(v) * x; }
    friend Modint operator/(ll v, const Modint & x) { return Modint(v) / x; }
    friend Modint operator^(ll v, const Modint & x) { return Modint(v) ^ x; }
    friend ostream & operator<<(ostream & os, const Modint & x) { return os << x.val; }
    friend istream & operator>>(istream & is, Modint & x) { ll v; is >> v; x = Modint(v); return is; }
};

const int inf = 1e9;
const ll infll = 1e18;
const double eps = 1e-8;
const ld epsll = 1e-12;
const int N = (1 << 20) + 5;
ll d[N], pre[N];
int main() {
    fast_io();
    int n;
    cin >> n;
    rep(i, 1, n) {
    	for (int j = i; j <= n; j += i) {
    		++d[j];
    	}
    }
    rep(i, 1, n) {
    	pre[i] = pre[i - 1] + d[i];
    }
    ll ans = 0;
    rep(x, 1, n - 1) {
    	ans += d[x] * pre[n - x]; 
    }
    cout << ans;
    return 0;
}
原文
存档评论1 条抓取于