Leetcode-1006 Clumsy Factorial(笨阶乘)
作者:互联网
1 #define pb push_back 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 const int maxn = 50003; 4 5 class Solution 6 { 7 public: 8 int clumsy(int N) 9 { 10 if(N==1) 11 return 1; 12 else if(N==2) 13 return 2; 14 else if(N==3) 15 return 6; 16 17 int tt = N*(N-1)/(N-2); 18 N -= 3; 19 int rnt = tt; 20 int flag = 1; 21 while(flag || N>3) 22 { 23 if(flag) 24 { 25 rnt += N; 26 if(N>4&&(N-1)*(N-2)%(N-3)==0) 27 tt -= 3; 28 else 29 tt -= 4; 30 N --; 31 32 flag = 0; 33 continue; 34 } 35 rnt -= tt; 36 37 N -= 3; 38 if(N>4&&(N-1)*(N-2)%(N-3)==0) 39 tt -= 3; 40 else 41 tt -= 4; 42 rnt += N; 43 N --; 44 } 45 if(N==1) 46 rnt -= 1; 47 else if(N==2) 48 rnt -= 2; 49 else if(N==3) 50 rnt -= 6; 51 return rnt; 52 } 53 };
数学题,A*(A-1)/(A-2)一定比(A-4)*(A-5)/(A-6)大3或4,具体是3还是4,主要看(A-4)*(A-5)是否能除尽(A-6),能除尽,那就差3,否则差4
标签:return,Factorial,tt,else,int,flag,阶乘,1006,rnt 来源: https://www.cnblogs.com/Asurudo/p/10504871.html