Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:52:10+00:00 2026-06-10T04:52:10+00:00

The problem is as follows: There is one friendly number and N unfriendly numbers.

  • 0

The problem is as follows:

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.   

Following is my C# solution:

class Solution
{
    static void Main(string[] args)
    {
        string firstLine = Console.ReadLine();
        string[] firstLineItems = firstLine.Split(' ');

        ulong friendlyNum = Convert.ToUInt64(firstLineItems[1]);
        int noOfunf = Convert.ToInt32(firstLineItems[0]);

        string secondLine = Console.ReadLine();
        string[] secondLineItems = secondLine.Split(' ');

        List<ulong> unfriendlyNumbersArray = new List<ulong>(noOfunf);

        foreach (string str in secondLineItems)
        {
            unfriendlyNumbersArray.Add(Convert.ToUInt64(str));
        }

        List<ulong> divisors = CalculateDivisors(friendlyNum);

        Console.WriteLine(finalCall(divisors, unfriendlyNumbersArray));
    }

    private static List<ulong> CalculateDivisors(ulong number)
    {
        List<ulong> factors = new List<ulong>();

        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;
    }

    private static long finalCall(List<ulong> divisors, List<ulong> unfriendlyNumbersArray)
    {
        long output = 0;
        var unfriendlyNumbers = (from i in unfriendlyNumbersArray select i).Distinct();

        foreach (ulong divisor in divisors)
        {
            var test = unfriendlyNumbers.Where(number => number % divisor == 0).ToList();
            output += (test.Count == 0) ? 1 : 0;
        }
        return output;
    }
}

Only first 3 test cases pass. 4th one is killed, 5th and 6th one gives me parsing exception for string.

Exception for 5th and 6th ones:

Unhandled Exception: System.FormatException: Input string was not in the correct format
    at System.UInt64.Parse (System.String s) [0x00000] in :0 
    at System.Convert.ToUInt64 (System.String value) [0x00000] in :0 
    at Solution.Main (System.String[] args) [0x00000] in :0 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in the     correct format
    at System.UInt64.Parse (System.String s) [0x00000] in :0 
    at System.Convert.ToUInt64 (System.String value) [0x00000] in :0 
    at Solution.Main (System.String[] args) [0x00000] in :0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:52:12+00:00Added an answer on June 10, 2026 at 4:52 am

    I was able to solve this problem based on the following link.
    I was really amazed by the in-depth understanding of number theory of the author of the above link.
    There is nothing wrong in the above algorithm. just that it will not solve the problem within 5 seconds, the constraint which we we have abide by.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql problem, my query looks as follows but not complete SELECT
Remark : I know there are many similar questions on SO, but none specific
My problem is as follows. I want to list all the file names in
I was trying to code a simple problem which goes as follows: This week
I'm trying to solve a Java synchronization problem. The case is as follows: There
I've started using databinding with MVVM pattern in Silverlight. There's one problem for me.
Certainly there is no one right way to do this, but I can't even
The problem runs as follows: if there are two strings str1 and str2 ,
This problem follows on from a previous question . When I run the following
My problem is as follows. I have an array of objects in the form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.