Advertisement
a53

Passwd

a53
Apr 26th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <map>
  8. #include <cmath>
  9. using namespace std;
  10. ifstream f("passwd.in");
  11. ofstream g("passwd.out");
  12. #define ll long long
  13. #define pb push_back
  14. #define mp make_pair
  15. #define sz size
  16. #define x first
  17. #define y second
  18. #define nmax 2501
  19. #define inf
  20. #define MOD 666013
  21. int tip, a, b, c, d;
  22. int X;
  23. int n, fact[nmax*4], inv[nmax*4];
  24. vector<char> v;
  25. int cntPasswd = 0;
  26. char Sol[nmax*4];
  27. bool gata = 0;
  28.  
  29. void citeste()
  30. {
  31. f >> tip;
  32. f >> a >> b >> c >> d;
  33. f >> X;
  34. }
  35.  
  36. int put(int a, int b)
  37. {
  38. // a ^ b;
  39. int rez = 1;
  40. for(; b; b>>=1){
  41. if (b&1) rez = (1LL*rez * a) % MOD;
  42. a = (1LL*a*a) % MOD;
  43. }
  44. return rez;
  45. }
  46.  
  47. void preprocesare()
  48. {
  49. fact[0] = 1; inv[0] = 1;
  50. fact[1] = 1; inv[1] = 1;
  51. for(int i=2; i<=n; ++i){
  52. fact[i] = (1LL*fact[i-1] * 1LL*i) % MOD;
  53. inv[i] = put(fact[i], MOD-2);
  54. //cout << i << " " << fact[i] << " " << inv[i] << "\n";
  55. }
  56. }
  57.  
  58. int comb(int n, int k)
  59. {
  60. if (n == k) return 1;
  61. if (k == 0) return 1;
  62. // comb(n,k) % MOD;
  63. //return (( 1LL*((1LL*fact[n]*inv[n-k])%MOD) * inv[k]) % MOD);
  64. int rez = ((1LL*fact[n] * inv[n-k]) % MOD * 1LL*inv[k]) % MOD;
  65. //cout << n << " " << k << " " << rez << "\n";
  66. return rez;
  67. }
  68.  
  69. void baga(int pos, int a, int b, int c, int d)
  70. {
  71. if (pos == n+1)
  72. {
  73. ++cntPasswd; if (cntPasswd == X)
  74. {
  75. gata = 1;
  76. for(int i=1; i<=n; ++i) g << Sol[i];
  77. g << "\n";
  78. return;
  79. }
  80. }
  81. else
  82. {
  83. if (a){
  84. for(char i='a'; i<='z'; ++i)
  85. {
  86. Sol[pos] = i;
  87. baga(pos+1, a-1, b, c, d);
  88. if (gata) return;
  89. }
  90. }
  91. if (b)
  92. {
  93. for(char i='A'; i<='Z'; ++i)
  94. {
  95. Sol[pos] = i;
  96. baga(pos+1, a, b-1, c, d);
  97. if (gata) return;
  98. }
  99. }
  100. if (c)
  101. {
  102. for(char i='0'; i<='9'; ++i)
  103. {
  104. Sol[pos] = i;
  105. baga(pos+1, a, b, c-1, d);
  106. if (gata) return;
  107. }
  108. }
  109. if (d)
  110. {
  111. for(int i=0; i<5; ++i)
  112. {
  113. Sol[pos] = v[i];
  114. baga(pos+1, a, b, c, d-1);
  115. if (gata) return;
  116. }
  117. }
  118. }
  119. }
  120.  
  121.  
  122. void primaCerinta()
  123. {
  124. v.pb('!'); v.pb('@'); v.pb('#'); v.pb('$'); v.pb('%');
  125. baga(1, a, b, c, d);
  126. }
  127.  
  128. void a2Cerinta()
  129. {
  130. preprocesare();
  131. int rez = (1LL*comb(n,a)*put(26,a))%MOD;
  132. rez = (1LL*rez*comb(n-a, b))%MOD;
  133. rez = (1LL*rez*put(26,b)) % MOD;
  134. rez = (1LL*rez*comb(n-a-b, c)) % MOD;// rez %= MOD;
  135. rez = (1LL*rez*put(10,c)) % MOD;// rez %= MOD;
  136. rez = (1LL*rez*comb(n-a-b-c, d)) % MOD;// rez %= MOD;
  137. rez = (1LL*rez*put(5,d)) %MOD;// rez %= MOD;
  138. //cout << rez << "\n";
  139. g << rez << "\n";
  140. }
  141.  
  142. void rezolva()
  143. {
  144. // solutie : pt prima cerinta generez toate solutiile incepand cu cea mai mica si ma opresc cand o gasesc pe a x-a solutie
  145. // a 2-a cerinta : fie n = a + b + c + d; => comb(n,a)*26^a * comb(n-a,b)*26^b * comb(n-a-b,c)*10^c * comb(n-a-b-c,d)*5^d;
  146. // imi preprocesez factorialele si inversele modulare
  147. n = a + b + c + d;
  148. if (tip == 1){
  149. primaCerinta();
  150. //for(int i=1; i<=n; ++i) cout << Sol[i];
  151. //cout << "\n";
  152. //g << "a" << "\n";
  153. //for(int i=1; i<=n; ++i) cout << sol[i];
  154. }
  155. else
  156. a2Cerinta();
  157. }
  158.  
  159.  
  160. int main()
  161. {
  162. citeste();
  163. rezolva();
  164. f.close();
  165. g.close();
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement