65,176
社区成员




#include <iostream>
#include <iomanip>
#include <cstdio>
#include <ctime>
struct Type
{
Type( unsigned h, unsigned m, unsigned l ) : high(h), middle(m), low(l)
{
}
Type( unsigned m, unsigned l ) : high(0), middle(m), low(l)
{
}
Type( unsigned l ) : high(0), middle(0), low(l)
{
}
static Type pow( unsigned base, unsigned power )
{
if( power==0 ) return Type(1);
unsigned n, m=1;
for( n=0; n<power && m*base<10000000; ++n )
m *= base;
unsigned a = 1;
for( unsigned i=0; i<power%n; ++i )
a *= base;
Type t = a;
for( unsigned i=0; i<power/n; ++i )
t *= m;
return t;
}
Type& operator+=( const Type& type )
{
low += type.low;
unsigned carry = low/10000000;
low%=10000000;
middle += type.middle + carry;
carry = middle/10000000;
middle %= 10000000;
high += type.high + carry;
return *this;
}
Type operator+( const Type& type ) const
{
Type ret = *this;
ret += type;
return ret;
}
Type& operator+=( unsigned n )
{
low += n;
unsigned carry = low/10000000;
low%=10000000;
middle += carry;
carry = middle/10000000;
middle %= 10000000;
high += carry;
return *this;
}
Type operator+( unsigned n ) const
{
Type ret = *this;
ret += n;
return ret;
}
Type& operator-=( const Type& type )
{
low = 10000000 + low - type.low;
unsigned carry = low/10000000;
low%=10000000;
middle = 10000000 + middle + carry - 1 - type.middle;
carry = middle/10000000;
middle %= 10000000;
high = high + carry - 1 - type.high;
return *this;
}
Type operator-( const Type& type ) const
{
Type ret = *this;
ret -= type;
return ret;
}
Type& operator*=( unsigned n )
{
unsigned long long tmp = 1ull*low*n;
unsigned long long carry = tmp/10000000;
low = unsigned(tmp%10000000);
tmp = 1ull*middle*n + carry;
carry = tmp/10000000;
middle = unsigned(tmp%10000000);
high = unsigned(high*n + carry);
return *this;
}
Type operator*( unsigned n ) const
{
Type ret = *this;
ret *= n;
return ret;
}
bool operator>( const Type& type ) const
{
if( high > type.high )
return true;
if( high < type.high )
return false;
if( middle > type.middle )
return true;
if( middle < type.middle )
return false;
return low>type.low;
}
bool operator>=( const Type& type ) const
{
if( high > type.high )
return true;
if( high < type.high )
return false;
if( middle > type.middle )
return true;
if( middle < type.middle )
return false;
return low>=type.low;
}
unsigned high, middle, low;
};
std::ostream& operator<<( std::ostream& os, const Type& type )
{
return os << std::setfill('0') << std::setw(7) << type.high
<< std::setfill('0') << std::setw(7) << type.middle
<< std::setfill('0') << std::setw(7) << type.low;
}
const Type C[10] = { Type::pow(0,21), Type::pow(1,21), Type::pow(2,21), Type::pow(3,21), Type::pow(4,21), Type::pow(5,21), Type::pow(6,21), Type::pow(7,21), Type::pow(8,21), Type::pow(9,21) };
const Type minval = Type( 1000000, 0000000, 0000000 );
const Type maxval = Type( 9999999, 9999999, 9999999 );
int main()
{
clock_t t0 = clock();
unsigned num[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; // 起码得有1个9,否则达不到21位;且,有了1个9后,至少为21位
Type overhead[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, maxval };
unsigned total[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
unsigned index = 9;
for( ; ; )
{
if( total[index]>21 || C[index]*num[index]>overhead[index] )
{
if( index == 9 )
break;
++index;
++num[index];
++total[index];
continue;
}
if( index == 1 )
{
Type val = maxval - overhead[index] + 1*num[index];
{ // 验证
unsigned t[3] = { val.low, val.middle, val.high };
unsigned m[10] = { 0 };
for( unsigned i=0; i!=3; ++i )
{
++m[ t[i]/1%10 ];
++m[ t[i]/10%10 ];
++m[ t[i]/100%10 ];
++m[ t[i]/1000%10 ];
++m[ t[i]/10000%10 ];
++m[ t[i]/100000%10 ];
++m[ t[i]/1000000%10 ];
}
unsigned f;
for( f=1; f<10 && m[f]==num[f]; ++f );
if( f == 10 )
{
std::cout << val << std::endl; // 未必是从小到大排序
}
}
++num[index];
++total[index];
continue;
}
overhead[index-1] = overhead[index] - C[index]*num[index];
total[index-1] = total[index];
--index;
num[index] = 0;
}
clock_t t1 = clock();
printf( "%d.%03d\n", (t1/CLOCKS_PER_SEC), (t1%CLOCKS_PER_SEC*1000) );
return 0;
}
// 128468643043731391252
// 449177399146038697307
// 1.625000
#include <utility>
#include <iostream>
#include <iomanip>
struct Type
{
Type( unsigned h, unsigned m, unsigned l ) : high(h), middle(m), low(l)
{
}
Type( unsigned m, unsigned l ) : high(0), middle(m), low(l)
{
}
Type( unsigned l ) : high(0), middle(0), low(l)
{
}
Type& operator+=( const Type& type )
{
low += type.low;
unsigned carry = low/10000000;
low%=10000000;
middle += type.middle + carry;
carry = middle/10000000;
middle %= 10000000;
high += type.high + carry;
return *this;
}
Type operator+( const Type& type ) const
{
Type ret = *this;
ret += type;
return ret;
}
Type& operator-=( const Type& type )
{
low = 10000000 + low - type.low;
unsigned carry = low/10000000;
low%=10000000;
middle = 10000000 + middle + carry - 1 - type.middle;
carry = middle/10000000;
middle %= 10000000;
high = high + carry - 1 - type.high;
return *this;
}
Type operator-( const Type& type ) const
{
Type ret = *this;
ret -= type;
return ret;
}
Type& operator*=( unsigned n )
{
unsigned long long tmp = 1ull*low*n;
unsigned long long carry = tmp/10000000;
low = unsigned(tmp%10000000);
tmp = 1ull*middle*n + carry;
carry = tmp/10000000;
middle = unsigned(tmp%10000000);
high = unsigned(high*n + carry);
return *this;
}
Type operator*( unsigned n ) const
{
Type ret = *this;
ret *= n;
return ret;
}
bool operator>( const Type& type ) const
{
if( high > type.high )
return true;
if( high < type.high )
return false;
if( middle > type.middle )
return true;
if( middle < type.middle )
return false;
return low>type.low;
}
bool operator>=( const Type& type ) const
{
if( high > type.high )
return true;
if( high < type.high )
return false;
if( middle > type.middle )
return true;
if( middle < type.middle )
return false;
return low>=type.low;
}
unsigned high, middle, low;
};
std::ostream& operator<<( std::ostream& os, const Type& type )
{
return os << std::setfill('0') << std::setw(7) << type.high
<< std::setfill('0') << std::setw(7) << type.middle
<< std::setfill('0') << std::setw(7) << type.low;
}
const Type C[10] = { Type( 0 )
, Type( 1 )
, Type( 2097152 )
, Type( 1046, 353203 )
, Type( 439804, 6511104 )
, Type( 4, 7683715, 8203125 )
, Type( 219, 3695064, 377856 )
, Type( 5585, 4586408, 3284007 )
, Type( 92233, 7203685, 4775808 )
, Type( 1094189, 8913151, 2359209 ) };
const Type minval = Type( 1000000, 0000000, 0000000 );
const Type maxval = Type( 9999999, 9999999, 9999999 );
// a*num^21>=minv && b*num^21>maxv && a<=residue && b<=residue+1
std::pair<unsigned,unsigned> foo( const Type& minv, const Type& maxv, unsigned num, unsigned residue )
{
unsigned a=residue+1, b=residue+1;
for( unsigned i=0; i<=residue+1; ++i )
{
Type val = C[num]*i;
if( a==residue+1 && val>=minv )
a = i;
if( val > maxv )
{
b = i;
break;
}
}
return std::pair<unsigned,unsigned>(a,b);
}
// 验证
bool bar( const Type& type, unsigned i9, unsigned i8, unsigned i7, unsigned i6, unsigned i5, unsigned i4, unsigned i3, unsigned i2, unsigned i1 )
{
unsigned t[3] = { type.low, type.middle, type.high };
unsigned m[10] = { 0, i1, i2, i3, i4, i5, i6, i7, i8, i9 };
for( unsigned i=0; i!=3; ++i )
{
--m[ t[i]/1%10 ];
--m[ t[i]/10%10 ];
--m[ t[i]/100%10 ];
--m[ t[i]/1000%10 ];
--m[ t[i]/10000%10 ];
--m[ t[i]/100000%10 ];
--m[ t[i]/1000000%10 ];
}
for( unsigned i=1; i<10; ++i )
if( m[i] != 0 )
return false;
return true;
}
int main()
{
Type l9 = maxval;
std::pair<unsigned,unsigned> r9 = foo( minval, l9, 9, 21 );
for( unsigned i9=r9.first; i9<r9.second; ++i9 )
{
Type l8 = l9 - C[9]*i9;
std::pair<unsigned,unsigned> r8 = foo( 0, l8, 8, 21-i9 );
for( unsigned i8=r8.first; i8<r8.second; ++i8 )
{
Type l7 = l8 - C[8]*i8;
std::pair<unsigned,unsigned> r7 = foo( 0, l7, 7, 21-i9-i8 );
for( unsigned i7=r7.first; i7<r7.second; ++i7 )
{
Type l6 = l7 - C[7]*i7;
std::pair<unsigned,unsigned> r6 = foo( 0, l6, 6, 21-i9-i8-i7 );
for( unsigned i6=r6.first; i6<r6.second; ++i6 )
{
Type l5 = l6 - C[6]*i6;
std::pair<unsigned,unsigned> r5 = foo( 0, l5, 5, 21-i9-i8-i7-i6 );
for( unsigned i5=r5.first; i5<r5.second; ++i5 )
{
Type l4 = l5 - C[5]*i5;
std::pair<unsigned,unsigned> r4 = foo( 0, l4, 4, 21-i9-i8-i7-i6-i5 );
for( unsigned i4=r4.first; i4<r4.second; ++i4 )
{
Type l3 = l4 - C[4]*i4;
std::pair<unsigned,unsigned> r3 = foo( 0, l3, 3, 21-i9-i8-i7-i6-i5-i4 );
for( unsigned i3=r3.first; i3<r3.second; ++i3 )
{
Type l2 = l3 - C[3]*i3;
std::pair<unsigned,unsigned> r2 = foo( 0, l2, 2, 21-i9-i8-i7-i6-i5-i4-i3 );
for( unsigned i2=r2.first; i2<r2.second; ++i2 )
{
Type l1 = l2 - C[2]*i2;
std::pair<unsigned,unsigned> r1 = foo( 0, l1, 1, 21-i9-i8-i7-i6-i5-i4-i3-i2 );
for( unsigned i1=r1.first; i1<r1.second; ++i1 )
{
Type l0 = l1-C[1]*i1;
Type val = maxval - l0;
if( bar(val,i9,i8,i7,i6,i5,i4,i3,i2,i1) )
{
// 未必是从小到大排序
std::cout << val << std::endl;
}
}
}
}
}
}
}
}
}
}
return 0;
}
//449177399146038697307
//128468643043731391252
/**
* 一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。
例如:
当N=3时,153就满足条件,因为 1^3 + 5^3 + 3^3 = 153,这样的数字也被称为水仙花数(其中,“^”表示乘方,5^3表示5的3次方,也就是立方)。
当N=4时,1634满足条件,因为 1^4 + 6^4 + 3^4 + 4^4 = 1634。
当N=5时,92727满足条件。
实际上,对N的每个取值,可能有多个数字满足条件。
程序的任务是:求N=21时,所有满足条件的花朵数。注意:这个整数有21位,它的各个位数字的21次方之和正好等于这个数本身。
如果满足条件的数字不只有一个,请从小到大输出所有符合条件的数字,每个数字占一行。因为这个数字很大,请注意解法时间上的可行性。要求程序在3分钟内运行完毕。
*/
package edu.excise.kaoshi;
import java.math.BigInteger;
import java.util.ArrayList;
public class Test10
{
public static final int SIZE = 24;
public static void main(String[] args)
{
MyStack myStack = new MyStack();
// String sStart = "10000000000";
// String sEnd = "99999999999";
String sStart = "1000000000";
String sEnd = "9999999999";
String mubiao;
int[] arr = new int[SIZE];
int[] sum = new int[SIZE];
int jieQu = 0;// 用来删除数组前n为零,以便与s比较
ArrayList list = new ArrayList();
BigInteger start = new BigInteger(sStart);
BigInteger end = new BigInteger(sEnd);
BigInteger ONE = new BigInteger("1");
for (BigInteger i = start; i.compareTo(end) < 0; i = i.add(ONE))
{
String s = i.toString();
mubiao = "";
// System.out.println(s + "的各项和为:");
// 入栈
for (int i1 = 0; i1 < s.length(); i1++)
{
myStack.push(s.charAt(i1));
}
// 出栈,进入数组
for (int i2 = myStack.index; i2 >= 0; i2--)
{
arr[SIZE - s.length() + i2] = myStack.pop();
}
// 求出次幂,并加入list,准备求加法
for (int i3 = 0; i3 < SIZE; i3++)
{
list.add(power(arr[i3], s.length()));
}
// 开始求和并且把list滞空,以便后面的数字调用
sum = getSum(list);
list.clear();
//获得求和后数组,并且截取前n为0,字符串
for (int i4 = 0; i4 < sum.length; i4++)
{
if (sum[i4] == 0)
{
} else
{
jieQu = i4;
break;
}
}
for (int i5 = jieQu; i5 < sum.length; i5++)
{
mubiao = mubiao + sum[i5];
}
//判断是否是花朵数
if (mubiao.equals(s))
{
System.out.println(s + "是一个花朵数字");
}
}
System.out.println("程序执行完毕");
}
public boolean isHuaDuoShu()
{
return false;
}
// 求21项的和
public static int[] getSum(ArrayList list)
{
int[] sum = new int[SIZE];
for (int i = 0; i < list.size(); i++)
{
sum = add(sum, (int[]) list.get(i));
// System.out.print("每次加完后的和为");
// for (int i5 = 0; i5 < sum.length; i5++)
// {
// System.out.print(sum[i5]);
// }
// System.out.println();
}
return sum;
}
// 每项的加法
public static int[] add(int sum[], int arr[])
{
int jinwei = 0;
for (int i = sum.length; i > sum.length - arr.length; i--)
{
int x = sum[i - 1] + arr[arr.length - sum.length + i - 1];
// 如果乘出的结果大于9,进位
if (x + jinwei > 9)
{
// 先用进位计算了本位结果之后,再赋给新的进位值
sum[i - 1] = (x + jinwei) % 10;
jinwei = (x + jinwei) / 10;
} else
{
sum[i - 1] = x + jinwei;
jinwei = 0;
}
}
return sum;
}
// 求幂次方
public static int[] power(int jishu, int zhishu)
{
int j = SIZE;// 数组大小
int jinwei = 0;// 防止相加的时机过早,加上这个参数
int[] arr = new int[j];// 数组
// for (int m = 0; m < SIZE; m++)
// arr[m] = 0;// 初始化
// 当指数为9时,刚传入,数组为空,先将最后一位赋值。
// i代表多少次方,j代表数组索引
for (int i = zhishu; i > 0; i--)
{
// 当第一次进入,复制
if (zhishu == i)
{
arr[j - 1] = jishu;
} else
{
// 进入循环,开始乘,遍历数组没一个元素
for (int p = SIZE; p > 0; p--)
{
// 每一位和基数乘后的值
int x = (arr[p - 1]) * jishu;
// 如果乘出的结果大于9,进位
if (x + jinwei > 9)
{
// 先用进位计算了本位结果之后,再赋给新的进位值
arr[p - 1] = (x + jinwei) % 10;
jinwei = (x + jinwei) / 10;
} else
{
arr[p - 1] = x + jinwei;
jinwei = 0;
}
}
}
}
return arr;
}
// 判断是否相等
public static boolean compareTo(int a[], int b[])
{
if (a.length == b.length)
{
for (int i = 0; i < a.length; i++)
{
if (a[i] != b[i])
{
return false;
}
}
} else
{
return false;
}
return true;
}
}
class MyStack
{
int[] arr = new int[Test10.SIZE];
int index = -1;
BigInteger num;
void push(char ch)
{
index++;
arr[index] = ch - 48;
}
int pop()
{
return arr[index--];
}
int getTop()
{
return arr[index];
}
}