GT and numbers(gcd)

本文探讨了如何通过算法解决特定数学问题,包括输入描述、输出描述、代码实现及实例解析,展示了从数学概念到实际编程应用的过程。

GT and numbers

 
 Accepts: 47
 
 Submissions: 939
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
给出两个数NNMMNN每次可以乘上一个自己的因数变成新的NN。
求最初的NNMM至少需要几步。
如果永远也到不了输出-11
输入描述
第一行读入一个数TT表示数据组数。
接下来TT行,每行两个数NNMMT\leq1000T1000, 1\leq N \leq 10000001N1000000,1 \leq M \leq 2^{63}1M263.

注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示答案。
输入样例
3
1 1
1 2
2 4
输出样例
0
-1
1
感觉数学题都不会做了,连是用gcd求解都不知道。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define T 100
#define eps 1e-10
typedef unsigned long long ll;
ll gcd(ll a,ll b)
{
	return a%b==0?b:gcd(b,a%b);
}
int main()
{
   /* freopen("input.txt","r",stdin); */
	ll N,n,m,k,ans;
	scanf("%I64d",&N);
	while(N--)
	{
		scanf("%I64d%I64d",&n,&m);
		ans=0;
		if(n==m){
			printf("0\n");
			continue;
		}
		while(n!=m)
		{
			if(m%n){
				printf("-1\n");break;
			}
			k = gcd(m/n,n);
			if(k==1){
				printf("-1\n");break;
			}
			n*=k,ans++;
		}
		if(n==m)
			printf("%I64d\n",ans);
	}
    return 0;
}


#include /-----------------------------------------------------------------------------------------------------------------------------------/ /* Type Definitions / /-----------------------------------------------------------------------------------------------------------------------------------*/ typedef unsigned char U1; /* 1-byte unsigned integer / typedef unsigned long long U2; / 8-byte unsigned integer */ /-----------------------------------------------------------------------------------------------------------------------------------/ /* Function Prototypes / /-----------------------------------------------------------------------------------------------------------------------------------*/ static void vd_s_CalculateFraction(U1 u1_n_input, U2* pu2_n_numerator, U2* pu2_n_denominator, U1 u1_step); static U2 u2_s_GetGCD(U2 u2_n_value1, U2 u2_n_value2); /-----------------------------------------------------------------------------------------------------------------------------------/ /* Main Function / / --------------------------------------------------------------------------------------------------------------------------------- / / Arguments: - / / Return: - / /-----------------------------------------------------------------------------------------------------------------------------------*/ int main(void) { U1 u1_t_input; /* User input: a positive integer / U2 u2_t_numerator, u2_t_denominator; / Fraction numerator and denominator */ while (1) /* Infinite loop for continuous input */ { printf("Please enter a positive integer n (or enter 0 to exit): "); scanf_s("%hhu", &u1_t_input); /* Read user input */ if (u1_t_input == 0) /* Exit condition */ { printf("Exiting the program. Goodbye!\n"); break; } if (u1_t_input &gt; 16) /* Limit input to avoid overflow */ { printf("Input is too large! Please enter a number less than or equal to 16.\n"); continue; } /* Determine step based on input: 2 for even numbers, 1 for odd numbers */ U1 u1_step = (u1_t_input % 2 == 0) ? 2 : 1; /* Calculate fraction using the common function */ vd_s_CalculateFraction(u1_t_input, &u2_t_numerator, &u2_t_denominator, u1_step); /* Simplify the fraction */ U2 u2_t_gcd = u2_s_GetGCD(u2_t_numerator, u2_t_denominator); /* Find greatest common divisor */ if (u2_t_gcd == 0 || u2_t_denominator == 0) /* Check for invalid denominator */ { printf("Error: Invalid fraction. Please try again.\n"); continue; } u2_t_numerator /= u2_t_gcd; u2_t_denominator /= u2_t_gcd; printf("The result is: %llu/%llu\n", u2_t_numerator, u2_t_denominator); /* Output the simplified fraction */ } return 0; } /-----------------------------------------------------------------------------------------------------------------------------------/ /* Calculate Fraction / / --------------------------------------------------------------------------------------------------------------------------------- / / Arguments: u1_n_input : User input: a positive integer / / pu2_n_numerator : Pointer to numerator / / pu2_n_denominator : Pointer to denominator / / u1_step : Step value (2 for even sequence, 1 for odd sequence) / / Return: - / /-----------------------------------------------------------------------------------------------------------------------------------*/ static void vd_s_CalculateFraction(U1 u1_n_input, U2* pu2_n_numerator, U2* pu2_n_denominator, U1 u1_step) { pu2_n_numerator = 0; / Initialize numerator to 0 */ pu2_n_denominator = 1; / Initialize denominator to 1 */ for (U1 u1_t_index = u1_step; u1_t_index <= u1_n_input; u1_t_index += u1_step) { *pu2_n_numerator = *pu2_n_numerator * u1_t_index + *pu2_n_denominator; /* Update numerator */ *pu2_n_denominator *= u1_t_index; /* Update denominator */ if (*pu2_n_denominator == 0) /* Check for overflow */ { printf("Error: Denominator overflow. Calculation stopped.\n"); return; } } } /-----------------------------------------------------------------------------------------------------------------------------------/ /* Get GCD / / --------------------------------------------------------------------------------------------------------------------------------- / / Arguments: u2_n_value1 : First integer / / u2_n_value2 : Second integer / / Return: Greatest common divisor / /-----------------------------------------------------------------------------------------------------------------------------------*/ static U2 u2_s_GetGCD(U2 u2_n_value1, U2 u2_n_value2) { while (u2_n_value2 != 0) { U2 u2_t_temp = u2_n_value1 % u2_n_value2; /* Calculate remainder / u2_n_value1 = u2_n_value2; / Update larger value / u2_n_value2 = u2_t_temp; / Update smaller value */ } return u2_n_value1; } /-----------------------------------------------------------------------------------------------------------------------------------/ /* Change History / /-----------------------------------------------------------------------------------------------------------------------------------/ / / / Version Date Author Change Description / / --------------- ---------- ------ ------------------------------------------------------------------------------------------- / / 1.1.0 2025.08.29 SJQ Refactored code to use a common pointer function for fraction calculation / /===================================================================================================================================*/ 吧这个代码修改成指针函数的
最新发布
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值