6.欧几里得的复仇!

本文介绍了一个基于扩展欧几里德算法的实际问题解决案例,该案例要求计算特定方程下所有正整数解的对数。文章通过提供详细的算法描述及样例输入输出,帮助读者理解并实现这一算法。

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

欧几里得要复仇!那么我们闲话少说,看看这个小兔崽子要干嘛吧~~

看题! 

                                                           英文的哦~

Description

In arithmetic and computer programming, the extended Euclidean algorithm is an extension to the Euclidean algorithm, which computes, besides the greatest common divisor of integers a and b, the coefficients of Bézout's identity, that is integers x and y such that ax + by = gcd(a, b). 
---Wikipedia 

Today, ex-Euclid takes revenge on you. You need to calculate how many distinct positive pairs of (x, y) such as ax + by = c for given a, b and c.

 

Input

The first line contains a single integer T, indicating the number of test cases. 

Each test case only contains three integers a, b and c. 

[Technical Specification] 
1. 1 <= T <= 100 
2. 1 <= a, b, c <= 1 000 000

 

Output

For each test case, output the number of valid pairs.

 

Sample Input


 

2

1 2 3

 1 1 4

 

Sample Output


 

1

3

                                                 好吧,看不懂,我们看中文的

描述

在算术和计算机编程中,扩展欧几里德算法是欧几里德算法的扩展,除了整数a和b的最大公约数之外,它还计算Bézout同一性的系数,即整数x和y,使得ax + by = gcd(a,b)。 
---维基百科 

今天,前欧几里德报复你。对于给定的a,b和c,您需要计算(x,y)有多少个不同的正对(例如ax + by = c)。

 

输入

第一行包含单个整数T,表示测试用例的数量。 

每个测试用例仅包含三个整数a,b和c。 

[技术规范] 
1. 1 <= T <= 100 
2. 1 <= a,b,c <= 1 000 000

 

产量

对于每个测试用例,输出有效对的数量。

 

样本输入


 

2

1 2 3

1 1 4

 

样本输出


 

1

3

然后我们来看一下代码:

if((c-a*j)%b==0)如果求余等于零,说明要求的数是整数,计数器加一。

if((c-a*j)<=0)  如果结果小于零,根本不符合,直接跳出。

#include<stdio.h>
#include<string.h>
int main()
{
    long long int a,b,c,t,i,j,k,s=0;
    scanf("%lld",&t);
    for(i=1;i<=t;i++)
    {
        s=0;
        scanf("%lld %lld %lld",&a,&b,&c);
        for(j=1;;j++)
        {
            if((c-a*j)<=0)
                break;

            if((c-a*j)%b==0)
                s++;
        }
        printf("%lld\n",s);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值