CF1928C Physical Education Lesson

原题链接

先考虑暴力枚举每个 \(k\) 是否合法,发现 \(k\) 合法当且仅当 \((2k-2)\mid (n-x)\) 或者 \((2k-2)\mid (n+x-2)\) 并且 \(k\geq x\)。因为当 \(n\) 处于每一段中的第 \(1\sim k\) 个数中时 \(n-x\) 是上一段的结尾,\(n\) 处于每一段中的第 \(k\sim 2k-2\) 个数中时 \(n+(x-2)\) 是这一段的结尾。

所以直接枚举 \(n-x\)\(n+(x-2)\) 的因数 \(p\),要保证 \(2\mid p\) 并且 \(\dfrac{p+2}{2}\geq x\)。注意可能有重复的,可以用 set 去重。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<math.h>
using namespace std;
int T,x,n;set <int> s;
inline void A(int y)
    {if(!(y&1)&&y/2+1>=x) s.insert(y);}
inline void work()
{
    cin>>n>>x;
    int a=n-x,b=n+(x-2);
    for(int i=1;i<=sqrt(a);++i)
        if(!(a%i)) A(i),A(a/i);
    for(int i=1;i<=sqrt(b);++i)
        if(!(b%i)) A(i),A(b/i);
    cout<<s.size()<<'\n';s.clear();
}
int main()
{
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(0);
    cin>>T;while(T--) work();
    return 0;
}
posted @ 2024-02-12 16:48  int_R  阅读(36)  评论(0编辑  收藏  举报