A. 爱喝「肥宅快乐水」的班长
题意:
有m种饮料,每种饮料无限瓶,要买n瓶饮料,其中一瓶必须为肥宅快乐水,请问有多少种购买方案?对1e+7 取模
题解:
题解一: 这道题就是求 n-1个瓶子 你可以放1~m种饮料 有多少种方案
比赛的时候我是直接把 m种饮料里面取i种的方案数 i种饮料放n-1个瓶子的方案数相乘得出答案
题解二:
来自官方的题解
隔板法,可以把m种不同的水当成m个不同的大水箱子(因为每一种水都有无限多),我们现在要把n个瓶子扔到m个不同的箱子里去,至少包含一瓶可乐即至少1个箱子有1个瓶子,利用隔板法,加入m- 1个这n个瓶子以外的瓶子,题目变成n + m- 1个相同的瓶子放入m个箱子,每个箱子至少-个瓶子,即n+m- 1排成一排,有n+m-2个空位,用m- 1个隔板分开。
答案就是
C
(
m
−
1
m
+
n
−
2
)
C \binom {m-1} {m+n-2}
C(m+n−2m−1)
隔板法真的巧妙
AC_code:
思路1
/*
Algorithm:
Author: anthony1314
Creat Time:
Time Complexity:
*/
#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
const int mod = 1e9+7;
using namespace std;
ll c[maxn][maxn];
void add(ll &x, ll y) {
x = (x + y) % mod;
}
void init() { //组合数预处理
/*
c[i][j] 代表 i个数里面取至少j个数字的方案数
*/
c[0][0] = 1;
for(int i = 1; i <= 2000; i++) {
c[i][0] = c[i][i] = 1;
for(int j = 1; j < i; j++) {
add(c[i][j], c[i-1][j-1]+c[i-1][j]);
}
}
}
int main() {
ll t, n, m;
init();
for(cin>>t; t; t--) {
ll ans = 0;
cin>>n>>m;
n = n-1;
if(n == 0 || m == 1){cout<<1<<endl; continue;}
int minn = min(n, m);
for(int i = 1; i <= minn; i++){
ll k = (c[m][i] * c[n-1][i-1]) % mod;
ans = (ans + k)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
思路2
/*
Algorithm:
Author: anthony1314
Creat Time:
Time Complexity:
*/
#include<bits/stdc++.h>
#define ll long long
#define maxn 2005
const int mod = 1e9+7;
using namespace std;
ll c[maxn][maxn];
void add(ll &x, ll y) {
x = (x + y) % mod;
}
void init() { //组合数预处理
/*
c[i][j] 代表 i个数里面取至少j个数字的方案数
*/
c[0][0] = 1;
for(int i = 1; i <= 2000; i++) {
c[i][0] = c[i][i] = 1;
for(int j = 1; j < i; j++) {
add(c[i][j], c[i-1][j-1]+c[i-1][j]);
}
}
}
int main() {
ll t, n, m;
init();
for(cin>>t; t; t--) {
ll ans = 0;
cin>>n>>m;
cout<<c[n+m-2][m-1]<<endl;
}
return 0;
}
B. 整数对
题意:
给定三个整数 n , m , p n,m,p n,m,p ,求满足 a × b = k × p a \times b = k \times p a×b=k×p ( ( ( 1 ≤ a ≤ n , 1 ≤ b ≤ m , k 1 \le a \le n, 1 \le b \le m, k 1≤a≤n,1≤b≤m,k为任意正整数 ) ) ) 的整数对 ( a , b ) (a,b) (a,b) 的数量。( 1 ≤ n , m ≤ 1 0 9 , 1 ≤ p ≤ 1 0 5 1 \le n,m \le 10^9, 1 \le p \le 10^5 1≤n,m≤109,1≤p≤105 )
题解:
要点:
rb 是 p 的倍数,即 b 要满足是 p/gcd(r, p) 的倍数
AC_code:
/*
Algorithm:
Author: anthony1314
Creat Time:
Time Complexity:
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n, m, p;
int t;
for(cin>>t; t; t--) {
cin>>n>>m>>p;
ll ans = 0;
ll x = n/p;
ans = x * m;//r取0的时候
for(ll r = 1; r < p; r++) {
ll nmp = n % p;
ll tmp = p / __gcd(r, p);
ll b = m / tmp;//b可以取的数量
if(r <= nmp) {
ans += (x+1) * b;
} else {
ans += x * b;
}
}
cout<<ans<<endl;
}
return 0;
}