cf1499D. The Number of Pairs

本文介绍了解决CF1499D问题的思路,通过分解a和b为互质因子并利用线性筛求不同质因子数量,快速求解每组(c,d,x)下满足条件的有序对(a,b)的数量。关键在于利用因子分解和数论技巧简化表达式,并进行高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cf1499D. The Number of Pairs

题意:

有t组询问,每组询问给定三个整数c,d,x
问有多少对(a,b)使得 c ∗ l c m ( a , b ) − d ∗ g c d ( a , b ) = x c*lcm(a,b)-d*gcd(a,b)=x clcm(a,b)dgcd(a,b)=x
1 < = t < = 1 e 4 , 1 < = c , d , x < = 1 e 7 1<=t<=1e4,1<=c,d,x<=1e7 1<=t<=1e4,1<=c,d,x<=1e7

题解:

可以这样设
a = p ∗ k , b = q ∗ k a=p*k,b=q*k a=pk,b=qk,那么 l c m ( a , b ) = p ∗ q ∗ k , g c d ( a , b ) = k lcm(a,b)=p*q*k,gcd(a,b)=k lcm(a,b)=pqkgcd(a,b)=k,其中p和q是互质的

将这些值带入数字中得: c ∗ p ∗ q ∗ k − d ∗ k = x c*p*q*k-d*k=x cpqkdk=x
两边同时除以k
c ∗ p ∗ q − d = x k c*p*q-d=\frac{x}{k} cpqd=kx
k必须被x整除这个式子才成立
x = t ∗ k x=t*k x=tk
那么 c ∗ p ∗ q − d = t c*p*q-d=t cpqd=t, t是x的因子
继续化简
p q = t + d c pq=\frac{t+d}{c} pq=ct+d,c必须是t+d的因子式子才成立
s = t + d c s=\frac{t+d}{c} s=ct+d
那么 p ∗ q = s p*q=s pq=s
我们之前已经定义了p和q是互质的,而p和q又是s的因子,因此就是看s有多少个不同的质因子数量,如果有num个,两两可以组成(a,b)对,因此就有 2 n u m 2^{num} 2num
如何求一个数有多少个不同的质因子数量,我们可以在线性筛的过程中求出
这样对于每次询问,我们先求出x的所有因子t,然后判断t+d是否是c的倍数,答案就是1<<((t+d)/c)
注意,线性筛要求2e7以内的,因为((t+d)/c)最大可以到2e7

代码:

// Problem: D. The Number of Pairs
// Contest: Educational Codeforces Round 106 (Rated for Div. 2)
// URL: https://codeforces.com/contest/{getProblemIndexes(problemCurrentPageList[i][0])[0]}/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{
    x= 0;
    char c= getchar();
    bool flag= 0;
    while (c < '0' || c > '9')
        flag|= (c == '-'), c= getchar();
    while (c >= '0' && c <= '9')
        x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();
    if (flag)
        x= -x;
    read(Ar...);
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#else
    startTime= clock();
    freopen("data.in", "r", stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#else
    endTime= clock();
    printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 2e7 + 20;
int prime[maxn];
int vis[maxn];
ll num[maxn];
int cnt= 0;
void Prime(int N)
{
    for (int i= 2; i <= N; i++) {
        if (!vis[i]) {
            prime[++cnt]= i;
            num[i]= 1;
        }
        for (int j= 1; j <= cnt && i * prime[j] <= N; j++) {
            vis[i * prime[j]]= 1;
            if (i % prime[j] == 0) {
                num[i * prime[j]]= num[i];
                break;
            }
            num[i * prime[j]]= num[i] + 1;
        }
    }
}
int fac[maxn];
signed main()
{
    //rd_test();
    int t;
    read(t);
    Prime(20000010);
    while (t--) {
        int c, d, x;
        read(c, d, x);
        int tot= 0;
        for (int i= 1; i * i <= x; i++) {
            if (x % i != 0)
                continue;
            fac[++tot]= i;
            if (i * i != x)
                fac[++tot]= x / i;
        }
        ll ans= 0;
        for (int i= 1; i <= tot; i++) {
            if ((fac[i] + d) % c == 0) {
                int s= (fac[i] + d) / c;
                ans+= (1ll << num[s]);
            }
        }
        cout << ans << endl;
    }
    //Time_test();
}

翻译:# CF1444A Division ## 题目描述 Oleg's favorite subjects are History and Math, and his favorite branch of mathematics is division. To improve his division skills, Oleg came up with $ t $ pairs of integers $ p_i $ and $ q_i $ and for each pair decided to find the greatest integer $ x_i $ , such that: - $ p_i $ is divisible by $ x_i $ ; - $ x_i $ is not divisible by $ q_i $ . Oleg is really good at division and managed to find all the answers quickly, how about you? ## 输入格式 The first line contains an integer $ t $ ( $ 1 \le t \le 50 $ ) — the number of pairs. Each of the following $ t $ lines contains two integers $ p_i $ and $ q_i $ ( $ 1 \le p_i \le 10^{18} $ ; $ 2 \le q_i \le 10^{9} $ ) — the $ i $ -th pair of integers. ## 输出格式 Print $ t $ integers: the $ i $ -th integer is the largest $ x_i $ such that $ p_i $ is divisible by $ x_i $ , but $ x_i $ is not divisible by $ q_i $ . One can show that there is always at least one value of $ x_i $ satisfying the divisibility conditions for the given constraints. ## 输入输出样例 #1 ### 输入 #1 ``` 3 10 4 12 6 179 822 ``` ### 输出 #1 ``` 10 4 179 ``` ## 说明/提示 For the first pair, where $ p_1 = 10 $ and $ q_1 = 4 $ , the answer is $ x_1 = 10 $ , since it is the greatest divisor of $ 10 $ and $ 10 $ is not divisible by $ 4 $ . For the second pair, where $ p_2 = 12 $ and $ q_2 = 6 $ , note that - $ 12 $ is not a valid $ x_2 $ , since $ 12 $ is divisible by $ q_2 = 6 $ ; - $ 6 $ is not valid $ x_2 $ as well: $ 6 $ is also divisible by $ q_2 = 6 $ . The next available divisor of $ p_2 = 12 $ is $ 4 $ , which is the answer, since $ 4 $ is not divisible by $ 6 $ .
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值