## 计蒜客2019蓝桥杯省赛A组模拟赛（一）题目及解析

### A. 结果填空：阶乘位数 题库链接

n n! 位数
1 1 1
2 2 1
3 6 1
4 24 2
5 120 3
6 720 3
7 5040 4
8 40320 5
9 362880 6
10 3628800 7

import java.math.BigInteger;

public class 阶乘位数 {

public static void main(String[] args) {
BigInteger n = BigInteger.valueOf(1);
BigInteger one =BigInteger.ONE;
BigInteger sum = BigInteger.ONE;
int len=0;
while(len<10000) {
sum = sum.multiply(n);
len = sum.toString().length();
System.out.println(n+" "+len);
}
System.out.println("答案"+n);
}

}

### B. 结果填空：炮台实验 题库链接

#include <cstdio>
using namespace std;
int main() {
double ans = 0;
int n = 2019;
for (int i = 1; i <= n; i++) {
ans += 1.0 / i;
}
printf("%.4f\n", ans);
return 0;
}

### D. 结果填空：修建公路 题库链接

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
int main() {
int n = 2019;
LL ans = 1;
for (int i = 1; i < n; i++) {
int t = 0;
for (int j = 0; i >> j > 0; j++) {
if (i >> j & 1) t++;
}
ans = ans * ((1 << t) - 1) % mod;
}
printf("%lld\n", ans);
return 0;
}

### E. 代码填空：欧拉函数 题库链接

#include <iostream>
using namespace std;
int euler(int n) {
int res = n;
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
res = res - res / i;
while (n % i == 0) {
n /= i;
}
}
}
return res;
}
int main() {
int n;
cin >> n;
cout << euler(n) << endl;
return 0;
}

### F. 程序设计：掎角之势 题库链接

#include <cmath>
#include <cstdio>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int x1, y1, x2, y2, x3, y3;
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
x1 -= x3;
y1 -= y3;
x2 -= x3;
y2 -= y3;
if (x1 * y2 == x2 * y1) {
puts("NO SOLUTION");
continue;
}
double s = fabs(x1 * y2 - x2 * y1);
double a = sqrt(x1 * x1 + y1 * y1);
double b = sqrt(x2 * x2 + y2 * y2);
double c = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double r1 = s / (a + b + c);
double r2 = a * b * c / s / 2;
printf("%.10f %.10f\n", r1 * r1 * acos(-1), r2 * r2 * acos(-1));
}
return 0;
}

#include <cmath>
#include <cstdio>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int x1, y1, x2, y2, x3, y3;
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
if (y1 == 0 || x2 == 0) {
puts("NO SOLUTION");
continue;
}
double s = y1 * x2;
double c = sqrt(y1 * y1 + x2 * x2);
double r1 = s / (y1 + x2 + c);
double r2 = c / 2;
printf("%.10f %.10f\n", r1 * r1 * acos(-1), r2 * r2 * acos(-1));
}
return 0;
}

### H. 程序设计：忽明忽暗 题库链接

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n, mod = 1e9 + 7;
cin >> n;
long long t = sqrt(n);
long long ans = t * (t + 1) / 2;
if (ans % 3 == 0) {
ans = ans / 3 % mod * (2 * t + 1) % mod;
} else {
ans = ans % mod * ((2 * t + 1) / 3) % mod;
}
cout << ans << endl;
return 0;
}
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n, mod = 1e9 + 7;
cin >> n;
__int128 t = sqrt(n);
long long ans = t * (t + 1) * (2 * t + 1) / 6 % mod;
cout << ans << endl;
return 0;
}
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
bool a[1000005];
int main() {
long long n, mod = 1e9 + 7;
cin >> n;
long long ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
a[j] = !a[j];
}
}
for (int i = 1; i <= n; i++) {
if (a[i]) ans += i;
}
cout << ans % mod << endl;
return 0;
}
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n, mod = 1e9 + 7;
cin >> n;
long long t = sqrt(n);
long long ans = 0;
for (long long i = 1; i * i <= n; i++) {
ans += i * i;
ans %= mod;
}
cout << ans << endl;
return 0;
}

### I. 程序设计：人以群分 题库链接

#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 500005;
int n, m;
int a[N], dp[N];
bool check(int x) {
// dp[0~m-1]=0
for (int i = m; i <= n; i++) {
if (a[i] - a[dp[i - m] + 1] <= x) {
dp[i] = i;
} else {
dp[i] = dp[i - 1];
}
}
return dp[n] == n;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
printf("%d\n", l);
return 0;
}

#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 500005;
int n, m;
int a[N];
int ans = 0;
void dfs(int u, int x, int t) {
if (u == n) {
if (u - x + 1 >= m) {
t = max(t, a[u] - a[x]);
ans = min(ans, t);
}
return;
}
if (u - x + 1 >= m) {
dfs(u + 1, u + 1, max(t, a[u] - a[x]));
}
dfs(u + 1, x, t);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
ans = 1e9;
dfs(1, 1, 0);
printf("%d\n", ans);
return 0;
}

暴力60%

#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 500005;
int n, m;
int a[N], dp[N];
bool check(int x) {
// dp[0~m-1]=0
for (int i = m; i <= n; i++) {
if (a[i] - a[dp[i - m] + 1] <= x) {
dp[i] = i;
} else {
dp[i] = dp[i - 1];
}
}
return dp[n] == n;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
for (int i = 0;; i++) {
if (check(i)) {
printf("%d\n", i);
break;
}
}
return 0;
}

### J. 程序设计：势能之和 题库链接

#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int N = 1000005;
int a[N];
LL two[N];
LL b[N];
LL inv[1000005];
void inv_init(int n) {
inv[1] = 1;
for (int i = 2; i <= n; i++) {
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
}
int main() {
const int wc = 1e6;
two[0] = 1;
for (int i = 1; i <= wc; i++) {
two[i] = two[i - 1] * 2 % mod;
}
inv_init(wc);
int n;
scanf("%d", &n);
LL all = 1;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
a[x]++;
all = all * x % mod;
}
LL ans = 0;
for (int i = wc; i >= 1; i--) {
int sum = a[i];
b[i] = 0;
for (int j = i + i; j <= wc; j += i) {
b[i] -= b[j];
sum += a[j];
}
b[i] += two[sum] - 1;
b[i] = (b[i] % mod + mod) % mod;
ans += b[i] * all % mod * inv[i] % mod;
}
ans %= mod;
printf("%lld\n", ans);
return 0;
}

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int N = 200005;
LL a[N];
int main() {
int n;
scanf("%d", &n);
LL all = 1;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
all = all * a[i];
}
for (int i = 0; i < n; i++) {
a[i] = all / a[i];
}
LL ans = 0;
for (int s = 1; s < 1 << n; s++) {
LL tmp = 1;
for (int i = 0; i < n; i++) {
if (s >> i & 1) {
tmp = tmp / __gcd(tmp, a[i]) * a[i];
}
}
ans = (ans + tmp) % mod;
}
printf("%lld\n", ans);
return 0;
}

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int N = 200005;
LL a[N];
LL pow_mod(LL x, LL p) {
LL res = 1;
while (p) {
if (p & 1) res = res * x % mod;
p >>= 1;
x = x * x % mod;
}
return res;
}
int main() {
int n;
scanf("%d", &n);
LL all = 1;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
all = all * a[i] % mod;
}
LL ans = 0;
for (int s = 1; s < 1 << n; s++) {
LL tmp = 0;
for (int i = 0; i < n; i++) {
if (s >> i & 1) {
tmp = __gcd(a[i], tmp);
}
}
ans = (ans + all * pow_mod(tmp, mod - 2)) % mod;
}
printf("%lld\n", ans);
return 0;
}

0条评论