Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2461326
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类:

2010-01-16 23:38:37

Fermat's Last Theorem
Submit: 460   Accepted:88
Time Limit: 4000MS  Memory Limit: 65536K
Description
In number theory, Fermat's Last Theorem states that no three positive integers a, b and c can satisfy the equation an + bn = cn for any integer value of n greater than two. This theorem was first conjectured by Pierre de Fermat in 1637, but was not proven until 1995 despite the efforts of many illustrious mathematicians. The unsolved problem stimulated the development of algebraic number theory in the 19th century and the proof of the modularity theorem in the 20th. It is among the most famous theorems in the history of mathematics.
xqq is very interested in Fermat’s Last Theorem. Of course, he is aware of its correctness. However, he regards it as a real pity that for any n > 2, an + bn = cn has no integer solutions. Now he is wondering whether there exists an equation: an + bm = cp, a , b , c , p must be positive integer. He will tell you a, b, n, m, please help him find the proper c and p. If there are more than one solutions, xqq only prefers the one with the least c and its corresponding p.


Input
There are multiple test cases. The first line of input is a positive integer t (1 <= t <= 10000), indicating the number of cases.
In each case, there are 4 positive integers a, b, n, m (1 <= a , b <= 1000000000, 1 <= n , m <= 30). We ensure that an + bm < 231.


Output
For each case, print the least c and proper p to satisfy the equation an + bm = cp.

Sample Input

3
1 2 1 3
3 4 2 2
4 4 4 4


Sample Output

3 2
5 2
2 9


/*因为数据范围在2^32次方以下,所以可以直接枚举,代码是参看别人的,在此记录一遍以后查看*/


#include<iostream>
#include<math.h>
using namespace std;
long long mypow(long long a,long long n)
{
    long long tmp=1;
    for(int i=1;i<=n;i++)
    {
       tmp*=a;
    }
    return tmp;
}
main()
{
      long long a,b,n,m,r,c,p;
      long long T;
      double tmp1,tmp2;
      cin>>T;
      while(T--)
      {
             cin>>a>>b>>n>>m;
             r=mypow(a,n)+mypow(b,m);
             for(p=32;p>=1;p--)
             {
               tmp1=1.0/p;
               c=pow(1.0*r,tmp1)+0.5;
               //printf("c:%d--1/p:%.2lf\n",c,tmp1);

               //printf("r:%d--mypow(c,p):%d\n",r,mypow(c,p));

               //cout<
               if(r==mypow(c,p))
                 break;
             }
             cout<<c<<" "<<p<<endl;
      }
}


阅读(909) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-07-06 22:56:02

想问一下为什么计算c的时候,后面要“+0.5”?