Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
memory limit exceed ,递归就这样阿 。。那位xd帮忙看看 多谢/*为什么memory limit exceed 递归那么费内存吗? 程序思路 num2,num5统计2,5的个数. result 为结果 对任意N,M (N+1)*....M 分解成为奇数列,和偶数列 奇数列用oddseq运算 奇数列中5的倍数数列找出 都处以5同时num5加上5的倍数数列个数 得到这个数列都是奇数列 调用odeeseq在此求解 奇数列不是5的倍数 必然是 1 3 7 9 11 13 17 19 。。。 对于这个 1*3*7*9 mod 10=9解决。 读于偶数列都处以2后,同时num2加上2的倍数数列个数 原问题变为求 first2*......*last2 的最后一位 这个又可以继续递归成奇数列 偶数列,按照上需思路解决 我测试了一些数据,结果很快,应该也都正确 但为什么超了memory 郁闷!!!!! */ #include <iostream> using namespace std; int num2,num5,result; void oddseq(int N,int M) { int low=N-M+1; int first5=low,last5=N; while(((first5%5)||(first5%10==0))&&first5<=N) first5++; if(first5<=N) { while((last5%5)||(last5%10==0)) last5--; num5+=(last5-first5)/10+1; oddseq(last5/5,last5/5-first5/5+1); } unsigned int firstodd=low%2?low:low+1; unsigned int lastodd=N; while((lastodd%10)!=(firstodd%10)) { lastodd--; } if(((lastodd-firstodd)/10)%2) result=(result*9)%10; while(lastodd<=N) { if(lastodd%5) result=(result*(lastodd%10))%10; lastodd+=2; } } void factorial(int N,int M) { int low=N-M+1,high=N; if(M==1) { while((N%2)==0) { num2++; N/=2; } while((N%5)==0) { num5++; N/=5; } result=(result*(N%10))%10; return; } int first2,last2; first2=low%2?(low+1)/2:low/2; last2=N/2; num2+=last2-first2+1; factorial(last2,last2-first2+1); oddseq(N,M); } void main() { unsigned int N,M; while(cin>>N>>M) { result=1; num2=0;num5=0; factorial(N,M); if(num5>num2) cout<<5<<endl; else if(num5==num2) cout<<result<<endl; else { switch((num2-num5)%4) { case 0: result = (result*6) % 10;break; case 1: result = (result*2) % 10;break; case 2: result = (result*4) % 10;break; case 3: result = (result*8) % 10;break; } cout<<result<<endl; } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator