fenwick123

Untitled

Jun 17th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /* Author : fenwick123 */
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main(){
  7. ios :: sync_with_stdio(false);
  8. cin.tie(nullptr);
  9. cout.tie(nullptr);
  10. int t;
  11. cin >> t;
  12. while (t--){
  13. int n ;
  14. long long k;
  15. cin >> n >> k;
  16. string s;
  17. cin >> s;
  18. map<char, long long> cnt;
  19. for (auto x : s){
  20. cnt[x]++;
  21. }
  22. long long ans = 0;
  23. vector<long long> fact;
  24. for (long long i = 1; i * i <= k ; i++){
  25. if (k % i == 0){
  26. fact.push_back (i);
  27. if (i * i != k){
  28. fact.push_back (k / i);
  29. }
  30. }
  31. }
  32. sort (fact.begin () , fact.end());
  33. multiset<long long> ms;
  34. for (auto x : cnt){
  35. ms.insert (x.second);
  36. }
  37.  
  38. auto works = [&] (int x , int y){
  39. long long av = 0;
  40. for (auto it = ms.rbegin(); it != ms.rend(); it++){
  41. av += (*it / x);
  42. }
  43. return av >= y;
  44. };
  45. for (int i = 0; i < (int) fact.size(); i++){
  46. long long size = fact[i];
  47. long long lo = 1 , hi = n;
  48. long long x = -1;
  49. while (lo <= hi){
  50. long long mid = (lo + hi) / 2;
  51. if (works (mid , size)){
  52. x = mid;
  53. lo = mid + 1;
  54. } else hi = mid - 1;
  55. }
  56. ans = max (ans , x * size);
  57. }
  58. cout << ans << endl;
  59. }
  60. }
Add Comment
Please, Sign In to add comment