Input
First line of input contains one integer c<=100 - number of test cases. Then c lines follow, each of them consisting of exactly one integer 0<=n<=10^12.
Output
For each test case output Yes if it is possible to represent given number as a sum of two squares and No if it is not possible.
Example
Input:
10
1
2
7
14
49
9
17
76
2888
27
Output:
Yes
Yes
No
No
Yes
Yes
Yes
No
Yes
No
https://www.spoj.pl/problems/TWOSQRS/
public class TwoSquaresOrNotTwoSquares
{
static int[] numPrimos;
public static void main( String args[] ) throws Exception
{
/**
* Mediante la criaba de Erastostenes calculamos todos los
* numeros primos menores que 10^12 ( 1000000 )
*/
boolean[] primes = new boolean[ 1000001 ];
for( int i = 2; i <= 1000; i ++ )
{
if( !primes[ i ] )
{
for( int j = i * i; j <= 1000000; j = j + i )
primes[ j ] = true;
}
}
/**
* Filtramos los numeros primos obtenidos mediante la criba,
* y los almacenarmos en un vector para su rapido acceso.
*/
numPrimos = new int[ 78498 ];
for( int i = 2, ind = 0; i <= 1000000; i ++)
{
if( !primes[ i ] )
numPrimos[ ind ++ ] = i;
}
long numero, numCasos = sig();
for( long i = 0; i < numCasos; i ++ )
{
numero = sig();
if( verificar( numero ) )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
public static boolean verificar( long num )
{
int limite = (int)Math.sqrt( num );
int exponente;
/**
* Existen 2 criterios para saber si un numero se puede representar
* como la suma de 2 cuadrados perfectos:
*
* 1.- Sea un numero no primo N, este se podra representar como la suma
* de 2 cuadrados perfectos solo si cada factor primo de la descomposicion
* de N que tenga un exponente impar tambien se puede representar como
* suma de 2 cuadrados perfectos. ( Esto esta basado en la teoria de anillos
* de enteros de Gauss ).
*
* 2.- Si el numero N es primo, por el teorema de Fermat, este se puede
* representar como una suma 2 cuadrados perfectos solo si:
*
* N % 4 = 1 ó N = 2
*/
for( int i = 0; i < numPrimos.length && numPrimos[ i ] <= limite; i ++ )
{
// Verificamos si el numero es divisible entre el primo actual
if( num % numPrimos[ i ] == 0 )
{
exponente = 0;
// Calculamos el exponente de este factor primo, al mismo tiempo
// que actualizamos el valor de num
do
{
exponente ++;
num = num / numPrimos[ i ];
}
while( num % numPrimos[ i ] == 0 );
// Si el exponente es impar, aplicamos el segundo criterio
if( exponente % 2 != 0 )
{
if( numPrimos[ i ] != 2 && numPrimos[ i ] % 4 != 1 )
return false;
}
limite = (int)Math.sqrt( num );
}
}
// Si el factor que aun queda es diferente de 1, aplicamos nuevamente el
// segundo criterio ( teorema de ferma )
if( num != 1 )
return( num == 2 || num % 4 == 1 );
else
return true;
}
public static long sig() throws Exception
{
long val = System.in.read() - 48;
int car = System.in.read();
while( car != 10 )
{
val = 10 * val + car - 48;
car = System.in.read();
}
return val;
}
}
1xbet korean bets | Licensed Bets.co.kr
ResponderEliminar1xbet korean bets 메리트 카지노 | Licensed 온카지노 Bets.co.kr. 18.1. For all new users. 1xbet korean The most trusted place to sign up.