Problem:
There is one friendly number and N unfriendly numbers. We want to find how many numbers are there which exactly divide the friendly number, but does not divide any of the unfriendly numbers.
Input Format:
The first line of input contains two numbers N and K seperated by spaces. N is the number of unfriendly numbers, K is the friendly number.
The second line of input contains N space separated unfriendly numbers.
Output Format:
Output the answer in a single line.
Constraints:
1 <= N <= 10^6
1 <= K <= 10^13
1 <= unfriendly numbers <= 10^18
Sample Input:
8 16
2 5 7 4 3 8 3 18
Sample Output:
1
Explanation :
Divisors of the given friendly number 16, are { 1, 2, 4, 8, 16 } and the unfriendly numbers are {2, 5, 7, 4, 3, 8, 3, 18}. Now 1 divides all unfriendly numbers, 2 divide 2, 4 divide 4, 8 divide 8 but 16 divides none of them. So only one number exists which divide the friendly number but does not divide any of the unfriendly numbers. So the answer is 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnFriendlyDemo
{
class Program
{
//private static int results = 0;
private static bool IsDividesUnFrnd = false;
private static List factors = new List();
private static List gcd = new List();
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(new char[] { ' ', '\t', '\n' });
int N = Int32.Parse(input[0]);
ulong K = ulong.Parse(input[1]);
ulong[] UnFrndNumbers = new ulong[N];
input = Console.ReadLine().Split(new char[] { ' ', '\t', '\n' });
for (int i = 0; i< N; i++)
{
UnFrndNumbers[i] = ulong.Parse(input[i]);
gcd.Add(GetGcd(K, UnFrndNumbers[i]));
}
//Array.Sort(UnFrndNumbers);
List divisor = CalculateDivisors(K);
var g=gcd.Distinct();
foreach (var gd in g)
{
for (int j = divisor.Count - 1; j >= 0; j--)
{
if (gd % divisor[j] == 0)
divisor.RemoveAt(j);
}
}
Console.WriteLine(divisor.Count);
Console.ReadKey();
}
private static List CalculateDivisors(ulong number)
{
factors = new List();
for (ulong factor = 1; factor * factor<= number; factor++)
{
if (number % factor == 0)
{
factors.Add(factor);
if (factor * factor != number)
{
factors.Add(number / factor);
}
}
}
return factors;
}
public static ulong GetGcd(ulong a, ulong b)
{
while (b != 0)
{
ulong m = a % b;
a = b;
b = m;
}
return a;
}
}
}
There is one friendly number and N unfriendly numbers. We want to find how many numbers are there which exactly divide the friendly number, but does not divide any of the unfriendly numbers.
Input Format:
The first line of input contains two numbers N and K seperated by spaces. N is the number of unfriendly numbers, K is the friendly number.
The second line of input contains N space separated unfriendly numbers.
Output Format:
Output the answer in a single line.
Constraints:
1 <= N <= 10^6
1 <= K <= 10^13
1 <= unfriendly numbers <= 10^18
Sample Input:
8 16
2 5 7 4 3 8 3 18
Sample Output:
1
Explanation :
Divisors of the given friendly number 16, are { 1, 2, 4, 8, 16 } and the unfriendly numbers are {2, 5, 7, 4, 3, 8, 3, 18}. Now 1 divides all unfriendly numbers, 2 divide 2, 4 divide 4, 8 divide 8 but 16 divides none of them. So only one number exists which divide the friendly number but does not divide any of the unfriendly numbers. So the answer is 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnFriendlyDemo
{
class Program
{
//private static int results = 0;
private static bool IsDividesUnFrnd = false;
private static List factors = new List();
private static List gcd = new List();
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(new char[] { ' ', '\t', '\n' });
int N = Int32.Parse(input[0]);
ulong K = ulong.Parse(input[1]);
ulong[] UnFrndNumbers = new ulong[N];
input = Console.ReadLine().Split(new char[] { ' ', '\t', '\n' });
for (int i = 0; i< N; i++)
{
UnFrndNumbers[i] = ulong.Parse(input[i]);
gcd.Add(GetGcd(K, UnFrndNumbers[i]));
}
//Array.Sort(UnFrndNumbers);
List divisor = CalculateDivisors(K);
var g=gcd.Distinct();
foreach (var gd in g)
{
for (int j = divisor.Count - 1; j >= 0; j--)
{
if (gd % divisor[j] == 0)
divisor.RemoveAt(j);
}
}
Console.WriteLine(divisor.Count);
Console.ReadKey();
}
private static List CalculateDivisors(ulong number)
{
factors = new List();
for (ulong factor = 1; factor * factor<= number; factor++)
{
if (number % factor == 0)
{
factors.Add(factor);
if (factor * factor != number)
{
factors.Add(number / factor);
}
}
}
return factors;
}
public static ulong GetGcd(ulong a, ulong b)
{
while (b != 0)
{
ulong m = a % b;
a = b;
b = m;
}
return a;
}
}
}
No comments:
Post a Comment