CF1928C Physical Education Lesson 题解

题意

  • 一种上下波动的数组,给出所在的位置 n 和对应的数字 x,求出有几种数组满足条件。令 k 为最大值,则数组长成这样子:

1,2,3,,k1,k,k1,k2,,2,1,2,3,

  • 如图,每 2(k1) 就循环一次。

分析

  • 因为每 2(k1) 个数就循环一次,因此可以列出同余方程 nx(mod2(k1)),若 x1,则还可以列出 n2kx(mod2(k1)),可以化成 n2x(mod2(k1))

  • 因此问题就转化成了怎么求解 ab(modx) 中的 x。转化一下得 ab0(modx),也即 xab,因此答案就是 ab 的偶数约数个数。复杂度 O(n)

  • 注意事项:如果 k=x 时成立的话,在两个式子里都会被统计一次,因此要注意判重。

AC 代码

#include <bits/stdc++.h>
#define int long long
#define inf 1e9
using namespace std;
int x, n;
inline int read(int &x) {
char ch = x = 0;
int m = 1;
while (ch < '0' || ch > '9') {
ch = getchar();
if (ch == '-')
m *= -1;
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - 48;
ch = getchar();
}
x *= m;
return x;
}
inline void print(int x) {
if (x < 0)
putchar('-'), x = -x;
static int stk[50];
int top = 0;
do {
stk[top++] = x % 10;
x /= 10;
} while (x);
while (top) {
putchar(stk[--top] + 48);
}
putchar('\n');
return ;
}
inline bool check(int i, int b, int x) {
return !(i & 1) && i > 2 && (i / 2 + 1) >= x && !(b != x && (i / 2 + 1) == x); //注意判重
}
inline int work(int a, int b, int x) {
int m = a - b, res = 0;
for (int i = 1; i * i <= m; i++) {
if (m % i == 0) {
if (check(i, b, x)) res++;
if (check(m / i, b, x)) res++; //记录答案
}
}
return res;
}
signed main() {
int T;
read(T);
while (T--) {
read(n), read(x);
int res = work(n, x, x);
if (x != 1) res += work(n, 2 - x, x); //x 不等于 1 时有另一种可能
if (!((n - x) & 1) && x <= 2) res++; //k=2的方案
print(res);
}
return 0;
}
  • 感谢围观!
posted @   HappyJaPhy  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示