由两个有限项的等差数列B, C, 求有多少个有限项的等差数列A,满足C是A, B的所有公共项,若有无穷个A满足条件,输出-1

该篇文章是关于使用C++编写的程序,实现了中国剩余定理的求解,主要涉及`__gcd`和`__lcm`函数,用于计算最大公约数和最小公倍数,解决特定类型的数学问题。

题目

思路:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
const int maxn = 1e6 + 5, inf = 1e9 + 5, maxm = 4e4 + 5, mod = 1e9 + 7, N = 1e6;
// int a[maxn], b[maxn];
int n, m;
string s;

int qpow(int a, int b){
    int res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int __lcm(int a, int b){
    int g = __gcd(a, b);
    return a * b / g;
}

void solve()
{
    int res = 0;
    int a, b, c, p, q, r, x, y, z;
    cin >> b >> q >> y >> c >> r >> z;
    int b2 = b + (y - 1) * q, c2 =  c + (z - 1) * r;
    if(!(b <= c && b2 >= c2 && r % q == 0 && (c - b) % q == 0)){
        cout << "0\n";
        return;
    }
    if(b > c - r || b2 < c2 + r){
        cout << "-1\n";
        return;
    }
    for(int i = 1; i * i <= r; i++){
        if(r % i == 0){
            if(__lcm(i, q) == r)
                res = (res + (r / i) * (r / i) % mod) % mod; 
        }
        if(r / i != i && __lcm(r / i, q) == r){
            int p = r / i;
            res = (res + r / p * (r / p) % mod) % mod;
        }
    }
    cout << res << '\n';
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    // fac[0] = 1;
    // for(int i = 1; i <= N; i++){
    //     fac[i] = fac[i - 1] * i % mod;
    // }
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__night_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值