UVA 11889-Benefit(数学_快速枚举因子)

Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans,
he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals
to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of his money. He believes that
finding such a number is hard enough that dissuades students from paying that.

You should write a program that help poor students giving the appropriate amount of money to Yaghoub. Of course if there are several answers you go for students‘ benefit which is the lowest of them.

Input

The first line begin with an integer T ( T100000),
the number of tests. Each test that comes in a separate line contains two integers A and C ( 1AC107).

Output

Print
the lowest integer B such that LCM(AB)
C in a single line. If no such integer exists, print "NO SOLUTION" instead. (Quotes
for clarity)

Sample Input

3
2 6
32 1760
7 16

Sample Output

3
55
NO SOLUTION
题意 :很简单 给出a,c求满足 lcm(a,b)==c 的最小整数b。没有则输出“NO SOLUTION”。
lcm(a,b)==a*b/gcd(a,b)==c --> a*b==gcd(a,b)*c; --> a/gcd(a,b)==c/b,因为a/gcd(a,b)肯定为整数,所以b肯定是c的因子,枚举c的因子即可。
一开始纯暴力枚举c的因子T了一发,才明白数学果然是王道。 枚举因子在判断素数的时候就有过优化,即只需要枚举到sqrt(c)。 还有一个优化条件是a必须是c的因子。因为
b/gcd(a,b)==c/a; 
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
ll gcd(ll a,ll b)
{
	if(b==0) return a;
	else return gcd(b,a%b);
}
void solve(ll a,ll c)
{
	// b/gcd(a,b)==c/a
	if(c%a)
	{
		puts("NO SOLUTION");
		return ;
	}
	ll b=1,ans=INF;
	int m=floor(sqrt(c)+0.5);
	while(b<=m)
	{
		if(c%b==0)
		{
			if(a*b==c*gcd(a,b))
			{
				ans=min(ans,b);
				break;
			}
			ll sb=c/b;
			if(a*sb==c*gcd(a,sb))
				ans=min(ans,sb);
		}
		b++;
	}
	if(ans!=INF)
		printf("%lld\n",ans);
	else
		puts("NO SOLUTION");
}
int main()
{
	int t;ll a,b,c;
	scanf("%d",&t);
	//   a/gcd(a,b)==c/b;
	while(t--)
	{
		scanf("%lld%lld",&a,&c);
		solve(a,c);
	}
	return 0;
}

时间: 2024-08-21 07:31:09

UVA 11889-Benefit(数学_快速枚举因子)的相关文章

Uva 11889 - Benefit( 数论 )

Uva 11889 - Benefit( 数论 ) 题意: calculate the lowest integer B such that LCM(A, B) = C 分析: LCM(A,B) = C = A*B/GCD(A,B)C*GCD(A,B) = A*BC/A = B/GCD(A,B)如果C%A != 0 无解否则, 令t = C/AB = t * GCD(A,B) 即B 一定是 t 的整数倍从t开始枚举B #include <cstdio> typedef long long LL

UVA 11889 Benefit

11889 Benefit 题意: 给出T(T ≤ 100000)组数据:每组数据中给出A,C (1 ≤ A, C ≤ 10e7):已知LCM(A,B)=C,求最小的B 如果无解的话,输出 NO SOLUTION 思路: 比较难懂,可以多思考几遍.首先b=c/a;如果b不是int;直接无解:然后循环(对A和B求GCD的一值,再A/=值)直到那个值为1: 反例:12 16 48,顺便借这个例子分析一下:b=c/a=48/12=4,12和4的LCM是12不是48因为他们的有GCD影响,然后GCD的那

Uva 11889 Benefit (lcm与gcd)

题意:给你两个数,a,c,求出 lcm(a,b)==c 时的 b 的最小值 思路:我们知道一个性质 gcd(a,b)*lcm(a,b) = a*b 由此我们可以得到 b = gcd(a,b)*lcm(a,b)/a 那我们可以先用 lcm(a,b)/a 计算出假定的b值 如果 gcd(a.b)==1 那么b的最小值确定 如果 gcd(a,b)!=1 我们就要通过计算来找到 计算方法为 a=a/gcd(a,b) b=b*gcd(a.b) 样例: 4 6 12 2 6 32 1760 7 16 结果:

UVA - 10892 LCM Cardinality (枚举因子)

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. Forexample 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs withLCM is equal to

UVA 10655 - Contemplation! Algebra(矩阵快速幂)

UVA 10655 - Contemplation! Algebra 题目链接 题意:给定p, q, n代表p=a+b,q=ab求an+bn 思路:矩阵快速幂,公式变换一下得到(an+bn)(a+b)=an+1+bn+1+ab(an?1+bn?1),移项一下得到an+1+bn+1=(an+bn)p?q(an?1+bn?1) 这样就可以用矩阵快速幂求解了 代码: #include <stdio.h> #include <string.h> long long p, q, n; str

Spring_MVC_教程_快速入门_深入分析

Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf SpringMVC核心配置文件示例.rar 作者:赵磊 博客:http://elf8848.iteye.com 目录 一.前言 二.spring mvc 核心类与接口 三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明

NSDictionary使用快速枚举方法

上一章我们通过一个枚举器访问并返回字典里的键值, 现在我们通过快速枚举来查找键对应的值. 涉及到的方法: objectForKey: 这个方法的意思是返回一个值给对应的键, 下面使用了两次这个方法, 第一次没在循环里面使用, 输入了值@"1", 对应的值就是@"One". PS: 在这里, 我们找到了键, 就代表找到了值. 下面是例子: #import <Foundation/Foundation.h> int main(int argc, const

ObjectC----字典类和集合类以及快速枚举和OC中的数组排序

// Create By 郭仔  2015年04月01日20:06:36 // 不可变字典 // 字典是适用于存放键值对的一种集合,里面的元素必须是对象类型 // 字典是无序的 // 字典赋值 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"guozai",@"name",@"nan",@"sex",@"14",@"

6、iOS快速枚举

今天在写程序的时候想在当前视图跳转的时候释放掉当前视图上面add的一些子视图.因为add的子视图有些是在别的类里面add进来的,当前页面不知道自己当前有哪几个类型的子视图.这样,我就想到了用循环遍历来查看当前视图有没有符合条件的子视图,如果有的话就释放掉. 我是这样写的: for(UIView * subView in self.view.subviews) { if([subView isKindOfClass:[XYZSeniorQueryView class]]) { [subView r