Tuesday, March 5, 2013

unfriendly numbers problem

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



    }
}

 

meeting point problem

problem:
There is an infinite integer grid where N people live in N different houses. They decide to create a meeting point at one person’s house.
From any given cell, all 8 adjacent cells are reachable in 1 unit of time, e.g. (x,y) can be reached from (x-1,y+1) in one unit of time. Find a common meeting place which minimizes the combined travel time of everyone.
Input Format
N for N houses.
The following N lines will contain two integers for the x and y coordinate of the nth house.
Output Format
M for the minimum sum of all travel times when everyone meets at the best location.
Constraints
N <= 105
The absolute value of each co-ordinate in the input will be at most 109
HINT: Please use long long 64-bit integers;
Input #1
4
0 1
2 5
3 1
4 0
Output #1
8
Explanation
The houses will have a travel-sum of 11, 13, 8 or 10. 8 is the minimum.
Input #2
6
12 -14
-3 3
-14 7
-14 -3
2 -12
-1 -6
Output #2:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Solution
{

    static string[] input = new string[2];
    static void Main(string[] args)
    {

        long N = Int64.Parse(Console.ReadLine());
        long[] X = new long[N];
        long[] Y = new long[N];
        long results = 0;
        double avg_x = 0;
        double avg_y = 0;
        double min = 0;
        int point = 0;
        for (int c = 0; c        {
            input = Console.ReadLine().Split(' ');
            X[c] = long.Parse(input[0]);
            Y[c] = long.Parse(input[1]);
        }
        for (int i = 0; i        {
            avg_x += (double)X[i] / N;
            avg_y += (double)Y[i] / N;
        }

        min = Math.Sqrt(((avg_x - X[0]) * (avg_x - X[0])) + ((avg_y - Y[0]) * (avg_y - Y[0])));
        for (int i = 1; i < N; i++)
        {
            double diff = Math.Sqrt(((avg_x - X[i]) * (avg_x - X[i])) + ((avg_y - Y[i]) * (avg_y - Y[i])));
            if (diff             {
                min = diff;
                point = i;
            }
        }
        for (int i = 0; i        {
            results += Math.Max((Math.Abs(X[point] - X[i])), (Math.Abs(Y[point] - Y[i])));
        }
        Console.WriteLine(results);
    }
}

 

String Similarity

problem:
String Similarity (25 Points)

For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings “abc” and “abd” is 2, while the similarity of strings “aaa” and “aaab” is 3.
Calculate the sum of similarities of a string S with each of it’s suffixes.
Input:
The first line contains the number of test cases T. Each of the next T lines contains a string each.

Output:
Output T lines containing the answer for the corresponding test case.

Constraints:
1 <= T <= 10
The length of each string is at most 100000 and contains only lower case characters.

Sample Input:
2
ababaa
aa

Sample Output:
11
3

Explanation:
For the first case, the suffixes of the string are “ababaa”, “babaa”, “abaa”, “baa”, “aa” and “a”. The similarities of each of these strings with the string “ababaa” are 6,0,3,0,1,1 respectively. Thus the answer is 6 + 0 + 3 + 0 + 1 + 1 = 11.

For the second case, the answer is 2 + 1 = 3.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace StringRedDemo
{
    class Solution
    {
        static int finalLength = 0;
        static void Main(string[] args)
        {
            int totalInputs = int.Parse(Console.ReadLine());
            for (int i = 0; i             {
                string strToProcess = Console.ReadLine();
                int totalstrLength = strToProcess.Length;
                finalLength = totalstrLength;
                char[] masterarraychar = strToProcess.ToCharArray();
                for (int k = 1; k                {

                    if (masterarraychar[0] != masterarraychar[k])
                        continue;
                    matchlen(masterarraychar, k);

                }
                Console.WriteLine(finalLength);
            }
            Console.ReadKey();
        }
        private static void matchlen(char[] masterarraychar, int k)
        {
            for (int j = 0; j < masterarraychar.Length - k; j++)
            {
                if (masterarraychar[j + k] == masterarraychar[j])
                {
                    finalLength++;
                }
                else
                {
                    break;
                }
            }
        }

    }
}
 

Monday, December 31, 2012

generate WSDL

Where can I find WSDL.exe?

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

At the command prompt:
WSDL http://ip address of the site/WebService/MathService.asmx /n:NameSp /out:FileName.cs


This will create a file called FileName.cs.

HTTP 405 during web service method call

  1. On IIS 7.5 -> YourWebsite -> Handler Mappings
  2. Choose "Add module mapping" option on the right side of the panel
  3. In "Request path" field enter *.wsdl
  4. In "Module" field enter "ProtocolSupportModule"
  5. Click on "Request restrictions" and go to Verbs tab
  6. Enter POST verb
  7. Save changes

Friday, November 9, 2012

command to stop a hung service



Query the process
To kill the service you have to know its PID or Process ID. To find this just type the following in at a command prompt:

sc queryex servicename 


Replace 'servicename' with the services registry name. For example: Print Spooler is spooler. (See Picture)



Identify the PID
After running the query you will by presented with a list of details. You will want to locate the PID. (Highlighted)



Run the Taskkill command
Now that you have the PID, you can run the following command to kill the hung process:

taskkill /f /pid [PID] 


Tuesday, September 4, 2012

Hosting WCF inside asp Virtual Directory

Step 1)

 VirtualDirectoryName->Properties->Virtual Directory Tab->Confirguration Button->

Insert new WildCard Mapping C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll 

Uncheck Verify that file exists

Step 2)

VirtualDirectoryName->Properties->Directory Security Tab->Authentication and access control->Edit Button->

Uncheck Integrated Windodws Authentication

Step3) 


Reset IIS