- Tsundoku">C - Tsundoku
- Sum of Divisors">D - Sum of Divisors
- NEQ">E - NEQ
AB水题,
C - Tsundoku
有两摞书,一摞有 $n$ 本,从上至下每本需阅读 $a_i$ 分钟,一摞有 $m$ 本,从上至下每本需阅读 $b_i$ 分钟,问最多能在 $k$ 分钟内读多少本书。
挺明显的前缀和处理,枚举从第一摞书中读多少本,余下的时间用二分查找能在第二摞书中读多少本。
ll n, m, k, a[1 << 18], b[1 << 18];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) cin >> a[i], a[i] += a[i - 1];
for (int i = 1; i <= m; ++i) cin >> b[i], b[i] += b[i - 1];
ll cnt = 0;
for (ll i = 0; i <= n; ++i)
if (k >= a[i]) cnt = max(cnt, upper_bound(b + 1, b + m + 1, k - a[i]) - b - 1 + i);
cout << cnt;
}
D - Sum of Divisors
设 $f{(x)}$ 为 $x$ 正因子个数,计算 $\sum\limits{i = 1}^n i\times f_{x}$
先筛出每个数的 %7D#card=math&code=f_%7B%28x%29%7D) 然后累加起来
const int N = 1e7 + 10;
ll a[N], n ,cnt;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) for (int j = i; j <= n; j += i) a[j] += 1;
for (int i = 1; i <= n; ++i) cnt += i * a[i];
cout << cnt;
}
E - NEQ
给出 $n,m$ 计算有多少个大小为 $n$ 的子序列 $a,b$ 满足以下条件
1.$1 \le a_i,b_i \le m$
2.$a_i \not= a_j if\ i\not= j$
3.$b_i \not= b_j if\ i\not= j$
4.$a_i \not= b_i$
没想出来,参考了一下其他的思路:
%5EiCn%5EiA%7Bm%20-%20i%7D%5E%7Bn%20-%20i%7D)%0A#card=math&code=Am%5En%28%5Csum%7Bi%20%3D%200%7D%5En%28-1%29%5EiCn%5EiA%7Bm%20-%20i%7D%5E%7Bn%20-%20i%7D%29%0A)
,
个数排
个位置,即合法的
的个数;
,对于每个合法的
来说,合法的
的个数;
%5Ei#card=math&code=%28-1%29%5Ei),由容斥定理;
,从
的
个位置中选
个位置与
中的数相等,余下
个位置共有
个数可选;
- 当
,
,即合法
的个数;
- 当
,
,即代表对
来说不合法
的个数;
- 所以右式即用容斥原理从合法的
中减去对
来说不合法的
的个数。
- 当
using ll = long long;
const int N = 5e5 + 10, mod = 1e9 + 7;
ll fac[N];
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1, a = a * a % mod)if (b & 1) ans = ans * a % mod;
return ans;
}
void init() {
fac[0] = 1;
for (int i = 1; i < N; ++i) fac[i] = fac[i - 1] * i % mod;
}
ll inv(ll n) {return qpow(n, mod - 2);}
ll A(ll n, ll m) {return fac[n] * inv(fac[n - m]) % mod;}
ll C(ll n, ll m) {return fac[n] * inv(fac[m]) % mod * inv(fac[n - m]) % mod;}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
init();
int n, m; cin >> n >> m;
ll sum = 0;
for (int i = 0; i <= n; ++i) {
sum += qpow(-1, i) * C(n, i) * A(m - i, n - i) % mod;
sum = (sum + mod) % mod;
}
cout << A(m, n) * sum % mod;
}