日期:2014-05-18  浏览次数:21000 次

请问上千亿次的运算如何优化?
以下是福利彩票“七乐彩”的两期对比中的一种算法。
共有2035800*2035800种!代码如下,运行需要10个小时。
请问如何优化。谢谢了!
C# code

int Up = 0, Dn = 0;
long[] jz = new long[2402];
int[] a = new int[4] { 0, 0, 0, 0 };
int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0, x7 = 0;
for (int i = 0; i < 2402; i++) jz[i] = 0;
for (int i1 = 0; i1 < 24; i1++)
  for (int i2 = i1 + 1; i2 < 25; i2++)
    for (int i3 = i2 + 1; i3 < 26; i3++)
      for (int i4 = i3 + 1; i4 < 27; i4++)
        for (int i5 = i4 + 1; i5 < 28; i5++)
          for (int i6 = i5 + 1; i6 < 29; i6++)
            for (int i7 = i6 + 1; i7 < 30; i7++){
             Up = 0;
             Up = Up | (1 << i1);
             Up = Up | (1 << i2);
             Up = Up | (1 << i3);
             Up = Up | (1 << i4);
             Up = Up | (1 << i5);
             Up = Up | (1 << i6);
             Up = Up | (1 << i7);
             for (int n1 = 0; n1 < 24; n1++)
               for (int n2 = n1 + 1; n2 < 25; n2++)
                 for (int n3 = n2 + 1; n3 < 26; n3++)
                   for (int n4 = n3 + 1; n4 < 27; n4++)
                     for (int n5 = n4 + 1; n5 < 28; n5++)
                       for (int n6 = n5 + 1; n6 < 29; n6++)
                         for (int n7 = n6 + 1; n7 < 30; n7++){
        x1 = 0; x2 = 0; x3 = 0; x4 = 0; x5 = 0; x6 = 0; x7 = 0;
        a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; 
        if (n1 > 0) { Dn = 1 << (n1 - 1); if ((Up & Dn) == Dn) x1++; }
        Dn = 1 << n1; if ((Up & Dn) == Dn) x1++;
        Dn = 1 << (n1 + 1); if ((Up & Dn) == Dn) x1++;
        Dn = 1 << (n2 - 1); if ((Up & Dn) == Dn) x2++;
        Dn = 1 << n2; if ((Up & Dn) == Dn) x2++;
        Dn = 1 << (n2 + 1); if ((Up & Dn) == Dn) x2++;
        Dn = 1 << (n3 - 1); if ((Up & Dn) == Dn) x3++;
        Dn = 1 << n3; if ((Up & Dn) == Dn) x3++;
        Dn = 1 << (n3 + 1); if ((Up & Dn) == Dn) x3++;
        Dn = 1 << (n4 - 1); if ((Up & Dn) == Dn) x4++;
        Dn = 1 << n4; if ((Up & Dn) == Dn) x4++;
        Dn = 1 << (n4 + 1); if ((Up & Dn) == Dn) x4++;
        Dn = 1 << (n5 - 1); if ((Up & Dn) == Dn) x5++;
        Dn = 1 << n5; if ((Up & Dn) == Dn) x5++;
        Dn = 1 << (n5 + 1); if ((Up & Dn) == Dn) x5++;
        Dn = 1 << (n6 - 1); if ((Up & Dn) == Dn) x6++;
        Dn = 1 << n6; if ((Up & Dn) == Dn) x6++;
        Dn = 1 << (n6 + 1); if ((Up & Dn) == Dn) x6++;
        Dn = 1 << (n7 - 1); if ((Up & Dn) == Dn) x7++;
        Dn = 1 << n7; if ((Up & Dn) == Dn) x7++;
        if (n7 < 30) { Dn = 1 << (n7 + 1); if ((Up & Dn) == Dn) x7++; }
        a[x1]++; a[x2]++; a[x3]++; a[x4]++; a[x5]++; a[x6]++; a[x7]++;
        //(a[0] << 8) + (a[0] << 6) + (a[0] << 3) - a[0]=a[0]*343=[0]*7*7*7
        //(a[1] << 5) + (a[1] << 4) + a[1]=a[1]*49=a[1]*7*7
        //(a[2] << 3) - a[2]=a[2]*7
        jz[((a[0]<<8)+(a[0]<<6)+(a[0]<<4)+(a[0]<<3)-a[0])+((a[1]<<5)+(a[1]<<4)+a[1])+((a[2]<<3)-a[2])+a[3]]++;
    }
}



------解决方案--------------------
探讨
以下是福利彩票“七乐彩”的两期对比中的一种算法。
共有2035800*2035800种!代码如下,运行需要10个小时。
请问如何优化。谢谢了!

C# code

int Up = 0, Dn = 0;
long[] jz = new long[2402];
int[] a = new int[4] { 0, 0, 0, 0 };
int x1 = 0, x2 = 0, x3 =……

------解决方案--------------------
探讨
崇祯评价他的木匠哥哥时说:亦一时精神之所寄也。35楼和36楼的两位大侠其实是不必这么认真的。

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