我的有道难题算法-双倍超立方数

      
 
Constraints
-  n取值范围为1到1,000,000,000(含)
Examples
0)  
      1

Returns: 0


1)  
      1729

Returns: 1
1729=1^3+12^3
1729=9^3+10^3


2)  
      475574

Returns: 27

因为题中大量使用3次方计算,这里我们也可以减少重复计算。原理公式为:

(n + 1)^2 = n^2 + 2n + 2
(n + 1)^3 = n^3 + 3n^2 + 3n + 1

所以我的实现算法如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    class Program
    {
        
static void Main(string[] args)
        {
            TwiceSuperCubic twice 
= new TwiceSuperCubic();
            System.Diagnostics.Stopwatch watch 
= new System.Diagnostics.Stopwatch();
            Dictionary
<int, List<int[]>> ret = null;
            watch.Start();
            
for (int i = 0; i < 1000; i++)
            {
                twice.CalcCount(
475574out ret);
            }

            watch.Stop();

            Console.WriteLine(
"======================");
            
foreach (KeyValuePair<int, List<int[]>> pair in ret)
            {
                
if (pair.Value.Count == 2)
                {
                    
foreach (int[] val in pair.Value)
                    {
                        Console.WriteLine(
"{0}^3 + {1}^3 = {2}", val[0], val[1], pair.Key);
                    }
                }
            }
            Console.WriteLine(
"======================");
            Console.WriteLine(twice.CalcCount(
1729));
            Console.WriteLine(twice.CalcCount(
475574));
            Console.WriteLine(
"用时:{0}毫秒", watch.ElapsedMilliseconds);

            Console.WriteLine(
"======================");
            Console.WriteLine(T2.count(
1729));
            Console.WriteLine(T2.count(
475574));
            watch.Start();
            
for (int i = 0; i < 1000; i++)
            {
                T2.count(
475574);
            }

            watch.Stop();
            Console.WriteLine(
"用时:{0}毫秒", watch.ElapsedMilliseconds);

            Console.ReadKey();

        }
    }

    
/// <summary>
    
/// evlon算法
    
/// </summary>
    public class TwiceSuperCubic
    {
        
private static List<int> pow2 = new List<int>(new int[]{0});
        
private static List<int> pow3 = new List<int>(new int[]{0});

        
public int CalcCount(int n)
        {
            Dictionary
<int, List<int[]>> debugInfo;
            
return CalcCount(n, out debugInfo);
        }

        
public int CalcCount(int n, out Dictionary<int, List<int[]>> debugInfo)
        {
            debugInfo 
= new Dictionary<int, List<int[]>>();
            
int maxi = Convert.ToInt32(Math.Pow(n, 1.0 / 3)) + 1;
            
for(int i = pow2.Count; i < maxi; ++i)
            {
                
int pos = i - 1;
                pow2.Add(pow2[pos] 
+ (pos << 1+ 1);         //  (n + 1)^2 = n^2 + 2n + 2
                pow3.Add(pow3[pos] + (pow2[pos] << 1+ pow2[pos] + (pos << 1+ pos + 1);     //  (n + 1)^3 = n^3 + 3n^2 + 3n + 1 
            }

            
int count = 0;

            
for(int x = 1; x < maxi; ++x)
            {
                
bool found = false;
                
for(int y = x; y < maxi; ++y)
                {
                    
int z = pow3[x] + pow3[y];
                    
if( z <= n)
                    {
                        
if (!debugInfo.ContainsKey(z))
                        {
                            debugInfo.Add(z, 
new List<int[]>(new int[][] { new int[] { x, y } }));
                            found 
= true;
                        }
                        
else
                        {
                            List
<int[]> val = debugInfo[z];
                            val.Add(
new int[] { x, y });

                            
if (val.Count == 2)
                                count
++;
                            
else
                                count
--;
                        }
                    }
                    
else
                    {
                        
break;
                    }
                }

                
if(!found)
                    
break;
            }


            
return count;
        }


    }

    
/// <summary>
    
/// ealget的算法 
    
/// </summary>
    class T2
    {
        
static List<int> _CubeCache = new List<int>();
        
static Dictionary<intint> _CubeCountDict = new Dictionary<intint>();

        
/**/
        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="n"></param>
        
/// <returns></returns>
        
/// <author>eaglet</author>
        static public int count(int n)
        {
            
if (n < 0)
            {
                
return 0;
            }

            _CubeCountDict.Clear();

            
//b <= n 的 3 次方根
            int max_b = (int)Math.Pow((double)n, 1f / 3);

            
//构造整数三次方计算缓存
            for (int i = _CubeCache.Count; i <= max_b; i++)
            {
                _CubeCache.Add(i 
* i * i);
            }


            
int count = 0;
            
int a = 1//正整数
            int b = a;
            
int sumCube;
            sumCube 
= _CubeCache[a] + _CubeCache[b];

            
do
            {
                
//计算 a^3+b^3
                while (sumCube <= n)
                {
                    
int hit;

                    
//看hash表中有没有已经存在的记录
                    if (_CubeCountDict.TryGetValue(sumCube, out hit))
                    {
                        
//存在,且只击中了1次,则计数加1
                        if (hit == 1)
                        {
                            count
++;
                        }

                        
//击中超过1次,计数减1
                        
//存在三组或以上组合相等的情况时走到这个分支
                        if (hit == 2)
                        {
                            count
--;
                        }

                        
//击中数加一
                        _CubeCountDict[sumCube]++;
                    }
                    
else
                    {
                        _CubeCountDict.Add(sumCube, 
1);
                    }

                    b
++;

                    
if (b > max_b)
                    {
                        
break;
                    }

                    sumCube 
= _CubeCache[a] + _CubeCache[b];
                }

                a
++;
                b 
= a;

                
if (b > max_b)
                {
                    
break;
                }

                sumCube 
= _CubeCache[a] + _CubeCache[b];
            }
            
while (sumCube <= n);

            
return count;
        }
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值