Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void Subtract(int A[500], int B[500])
- /* A <- A-B */
- {
- int i, T=0;
- for (i=B[0]+1; i<=A[0];) B[i++]=0;
- for (i=1; i<=A[0]; i++)
- //A[i]+= (T=(A[i]-=B[i]+T)<0) ? 10 : 0;
- {
- A[i]=A[i]-(B[i]+T);
- if (A[i]<0) T=1;
- else T=0;
- if (T) A[i]+=10;
- }
- while (!A[A[0]]) A[0]--;
- }
- void Mult(int H[300], unsigned long long X)
- /* H <- H*X */
- {
- int i;
- unsigned long T=0;
- for (i=1; i<=H[0]; i++)
- {
- H[i]=H[i]*X+T;
- T=H[i]/10;
- H[i]=H[i]%10;
- }
- while (T) /* Cat timp exista transport */
- {
- H[++H[0]]=T%10;
- T/=10;
- }
- }
- ifstream f("raganama.in");
- ofstream g("raganama.out");
- int ap[50],c,n,v[105],A[500],B[500],factori[500],maxx;
- char s[105],s1[105];
- int main()
- {
- f>>c;
- if(c==1)
- {
- f>>s;
- n=strlen(s);
- strcpy(s1,s);
- sort(s,s+n);
- int gasit=0;
- while(gasit==0)
- {
- if(strcmp(s,s1)!=0)
- {
- g<<s;
- gasit=1;
- }
- else
- {
- f>>s1;
- next_permutation(s,s+n);
- }
- }
- }
- else
- {
- f>>s;
- n=strlen(s);
- //formula pt cate anagrame sunt in total e n!/Produs(ap[i]!)
- for(int i=0; i<n; i++)
- ap[s[i]-'a']++;
- unsigned long long k=1;
- while(f>>s) //cate anagrame sunt deja
- k++;
- A[0]=1;
- A[1]=1;
- B[0]=0;
- B[1]=0;
- int lung=1;
- while(k) // pun nr de anagrame citite intr-un vector
- {
- B[lung]=k%10;
- B[0]++;
- k/=10;
- lung++;
- }
- for(int i=2; i<=n; i++) //calculez n! - pun in factori[i] de cate ori apare fiecare factor din descomp lui n!
- {
- int p=0;
- int x=i;
- while(x%2==0)
- {
- x/=2;
- p++;
- }
- factori[2]+=p;
- int j=3;
- while(x!=1)
- {
- p=0;
- while(x%j==0)
- {
- x/=j;
- p++;
- }
- if(p>0)
- {
- factori[j]+=p;
- if(j>maxx) //retin cel mai mare factor prim
- maxx=j;
- }
- j+=2;
- if(j*j>x)
- j=x;
- }
- }
- for(int i=0; i<=26; i++) // scad din factori pe cei care apar in Prod(ap[i]!)
- {
- if(ap[i]>1)
- {
- for(int d=2; d<=ap[i]; d++)
- {
- int p=0;
- int x=d;
- while(x%2==0)
- {
- x/=2;
- p++;
- }
- factori[2]-=p;
- int j=3;
- while(x!=1)
- {
- p=0;
- while(x%j==0)
- {
- x/=j;
- p++;
- }
- factori[j]-=p;
- j+=2;
- if(j*j>x)
- j=x;
- }
- }
- }
- }
- for(int i=0; i<=maxx; i++)
- {
- while(factori[i]>0)
- {
- Mult(A,i);
- factori[i]--;
- }
- }
- Subtract(A,B);
- for(int i=A[0]; i>0; i--)
- g<<A[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement