日期:2014-05-20 浏览次数:21069 次
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        class Node
        {
            public int Num { get; set; }
            public Dictionary<int, int> Count { get; set; }
            public Node() { Count = new Dictionary<int, int>(); }
            public override string ToString()
            {
                string s = "";
                foreach (var i in Count)
                {
                    s += "|" + Math.Pow((double)Num, (double)i.Key).ToString() + " " + i.Value.ToString();
                }
                return s;
            }
        }
        static Dictionary<int, Node> Nodes = new Dictionary<int, Node>();
        static List<int> Primers = new List<int>() { };
        static void ProcPrimes(int num)
        {
            bool noprime = false;
            foreach (var i in Primers)
            {
                if (num % i == 0)
                {
                    noprime = true;
                    break;
                }
                else
                {
                    if (i > num) break;
                }
            }
            if (!noprime) Primers.Add(num);
        }
        static void ToNodes(int num, int power)
        {
            List<int> list = new List<int>();
            foreach (var i in Primers)
            {
                if (num % i == 0)
                {
                    list.Add(i);
                    break;
                }
                else
                {
                    if (i > num) break;
                }
            }
            int x = 1;
            list.ForEach(x1 => x *= x1);
            if (!Nodes.ContainsKey(x)) Nodes.Add(x, new Node(){ Num = x });
            int pwr = num / x * power;
            if (!Nodes[x].Count.ContainsKey(pwr)) 
                Nodes[x].Count.Add(pwr, 1);
            else
                Nodes[x].Count[pwr]++;
        }
        static int Solve(int startA, int endA, int startB, int endB)
        {
            Debug.Assert(startA > 1 && startB > 1);
            Debug.Assert(endA > startA && endB > startB);
            for (int i = 2; i <= endA; i++)
                ProcPrimes(i);
            for (int i = startA; i <= endA; i++)
                for (int j = startB; j <= endB; j++)
                    ToNodes(i, j);
            Nodes.Values.ToList()
                .ForEach(x => (from y
                               in x.Count
                               where y.Value > 0
                               select Math.Pow((double)x.Num, (double)y.Key))
                                .ToList().ForEach(z => Debug.WriteLine(z)));
            int n = 0;
            Nodes.ToList().ForEach(x => n += x.Value.Count.Count);
            return n;            
        }
        static void Main(string[] args)
        {
            Console.WriteLine(Solve(2, 1000, 2, 1000));
        }
        
    }
}