# 在Java区看到一个比较有意思的题目，转过来大家讨论一下,该如何处理

www.MyException.Cn  网友分享于：2013-03-17  浏览：11次

Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2^2=4, 2^3=8, 2^4=16, 2^5=32
3^2=9, 3^3=27, 3^4=81, 3^5=243
4^2=16, 4^3=64, 4^4=256, 4^5=1024
5^2=25, 5^3=125, 5^4=625, 5^5=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

.......

（比如4^3 = 8^2，这样的话就有重复了)

------解决方案--------------------

C# code
```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;
}
}
}

static void ToNodes(int num, int power)
{
List<int> list = new List<int>();
foreach (var i in Primers)
{
if (num % i == 0)
{
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))
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));
}

}
}
```