5
1.题目大意:
计算 的正方形中长方形的个数(不包括正方形)。
2.思路:
递推:
令 表示前 行共有多少个矩形(包括正方形), 表示前 行共有多少个正方形,则 。
由画图可知,用上半部分加下半部分加合并部分即可,,
。
由画图可知,用上半部分加下半部分加合并部分计算:
,
ps:注意中间部分不是从 加到 而是到 。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1e5 + 50;
ull n;
ull f[N], g[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("rec.in", "r", stdin);
// freopen("rec.out", "w", stdout);
cin >> n;
f[1] = (n * n - n) / 2 + n;
g[1] = n;
for (ull i = 2; i <= n; ++i) {
f[i] = f[1] + f[i - 1] + f[1] * (i - 1);
g[i] = g[i - 1] + g[1] + n * (i - 1) - (i * i - i) / 2;
}
cout << f[n] - g[n];
return 0;
}ps:好像要开**__int128**。
第 1 题: square 平方数
1.题目大意:
求一个数的最大的平方约数。
2.思路:
枚举平方数。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("square.in", "r", stdin);
// freopen("square.out", "w", stdout);
cin >> n;
for (ll i = (ll)sqrt(n); i >= 1; --i) {
if (n % (i * i) == 0) {
cout << i * i;
return 0;
}
}
return 0;
}2
1.题目大意:
从 个人中选出 个人排队打饭,求排列方案数。
2.思路:
求 。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
ll n, m, res = 1;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("lineup.in", "r", stdin);
// freopen("lineup.out", "w", stdout);
cin >> n >> m;
for (ll i = n; i >= n - m + 1; --i) {
(res *= (i % MOD)) %= MOD;
}
cout << res;
return 0;
}ps: 错因:
未对 取模。
3
1.题目大意:
求第 个优美数。
(优美数指含有 或是 的倍数的数)
2.思路:
打表(离线)。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
ll t, k[N], cnt, ans, n;
ll a[N];
bool check(ll x) {
if (x % 7 == 0) return 1;
while (x) {
if (x % 10 == 7) return 1;
x /= 10;
}
return 0;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("beauty.in", "r", stdin);
// freopen("beauty.out", "w", stdout);
cin >> t;
for (int i = 1; i <= t; ++i) cin >> k[i], n = max(n, k[i]);
for (int i = 1; cnt < n; ++i) {
if (check(i)) {
a[++cnt] = i;
}
}
for (int i = 1; i <= t; ++i) {
cout << a[k[i]] << "\n";
}
return 0;
}第 4 题: hundred 倍数
1.题目大意:
在数组 中找到两个数使得差为 的倍数(包括 )。
2.思路:
将 对 取模,存入桶中,对于每个数量 多于 的桶累加 的贡献。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 150;
ull n, a[N];
ull ans;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen("hundred.in", "r", stdin);
// freopen("hundred.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; ++i) {
int cn;
cin >> cn;
a[cn % 100]++;
}
for (int i = 0; i < 100; ++i) {
if (a[i] > 1) {
ans += a[i] * (a[i] - 1) / 2;
}
}
cout << ans;
return 0;
}ps错因:
未使用桶记录。
6
1.题目大意:
求阶乘的约数个数。
2.思路:
唯一分解后将所有指数加 后累乘。
3.代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1e5 + 50;
int n;
int a[150];
ull ans = 1;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int man = 2; man <= n; ++man) {
int qq = man;
for (int i = 2; i <= qq / i; ++i) {
if (qq % i == 0) {
while (qq % i == 0) {
a[i]++;
qq /= i;
}
}
}
if (qq > 1) {
a[qq]++;
}
}
for (int i = 1; i <= 100; ++i) {
ans *= (a[i] + 1);
}
cout << ans;
return 0;
}