Advertisement
a53

raganama

a53
May 3rd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. void Subtract(int A[500], int B[500])
  4. /* A <- A-B */
  5. {
  6. int i, T=0;
  7.  
  8. for (i=B[0]+1; i<=A[0];) B[i++]=0;
  9. for (i=1; i<=A[0]; i++)
  10. //A[i]+= (T=(A[i]-=B[i]+T)<0) ? 10 : 0;
  11. {
  12. A[i]=A[i]-(B[i]+T);
  13. if (A[i]<0) T=1;
  14. else T=0;
  15. if (T) A[i]+=10;
  16. }
  17. while (!A[A[0]]) A[0]--;
  18.  
  19. }
  20. void Mult(int H[300], unsigned long long X)
  21. /* H <- H*X */
  22. {
  23. int i;
  24. unsigned long T=0;
  25.  
  26. for (i=1; i<=H[0]; i++)
  27. {
  28. H[i]=H[i]*X+T;
  29. T=H[i]/10;
  30. H[i]=H[i]%10;
  31. }
  32. while (T) /* Cat timp exista transport */
  33. {
  34. H[++H[0]]=T%10;
  35. T/=10;
  36. }
  37. }
  38. ifstream f("raganama.in");
  39. ofstream g("raganama.out");
  40.  
  41. int ap[50],c,n,v[105],A[500],B[500],factori[500],maxx;
  42. char s[105],s1[105];
  43.  
  44. int main()
  45. {
  46. f>>c;
  47. if(c==1)
  48. {
  49. f>>s;
  50. n=strlen(s);
  51. strcpy(s1,s);
  52. sort(s,s+n);
  53. int gasit=0;
  54. while(gasit==0)
  55. {
  56. if(strcmp(s,s1)!=0)
  57. {
  58. g<<s;
  59. gasit=1;
  60. }
  61. else
  62. {
  63. f>>s1;
  64. next_permutation(s,s+n);
  65. }
  66. }
  67. }
  68. else
  69. {
  70. f>>s;
  71. n=strlen(s);
  72. //formula pt cate anagrame sunt in total e n!/Produs(ap[i]!)
  73. for(int i=0; i<n; i++)
  74. ap[s[i]-'a']++;
  75.  
  76. unsigned long long k=1;
  77. while(f>>s) //cate anagrame sunt deja
  78. k++;
  79. A[0]=1;
  80. A[1]=1;
  81. B[0]=0;
  82. B[1]=0;
  83. int lung=1;
  84. while(k) // pun nr de anagrame citite intr-un vector
  85. {
  86. B[lung]=k%10;
  87. B[0]++;
  88. k/=10;
  89. lung++;
  90. }
  91. for(int i=2; i<=n; i++) //calculez n! - pun in factori[i] de cate ori apare fiecare factor din descomp lui n!
  92. {
  93.  
  94. int p=0;
  95. int x=i;
  96.  
  97. while(x%2==0)
  98. {
  99. x/=2;
  100. p++;
  101. }
  102. factori[2]+=p;
  103. int j=3;
  104. while(x!=1)
  105. {
  106. p=0;
  107. while(x%j==0)
  108. {
  109. x/=j;
  110. p++;
  111. }
  112. if(p>0)
  113. {
  114. factori[j]+=p;
  115. if(j>maxx) //retin cel mai mare factor prim
  116. maxx=j;
  117. }
  118. j+=2;
  119. if(j*j>x)
  120. j=x;
  121. }
  122. }
  123. for(int i=0; i<=26; i++) // scad din factori pe cei care apar in Prod(ap[i]!)
  124. {
  125. if(ap[i]>1)
  126. {
  127. for(int d=2; d<=ap[i]; d++)
  128. {
  129. int p=0;
  130. int x=d;
  131. while(x%2==0)
  132. {
  133. x/=2;
  134. p++;
  135. }
  136. factori[2]-=p;
  137.  
  138. int j=3;
  139. while(x!=1)
  140. {
  141. p=0;
  142. while(x%j==0)
  143. {
  144. x/=j;
  145. p++;
  146. }
  147. factori[j]-=p;
  148. j+=2;
  149. if(j*j>x)
  150. j=x;
  151. }
  152. }
  153. }
  154. }
  155. for(int i=0; i<=maxx; i++)
  156. {
  157. while(factori[i]>0)
  158. {
  159. Mult(A,i);
  160. factori[i]--;
  161. }
  162. }
  163. Subtract(A,B);
  164.  
  165. for(int i=A[0]; i>0; i--)
  166. g<<A[i];
  167. }
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement