1470B - Strange Definition - CodeForces Solution


bitmasks graphs hashing math number theory *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define whilst while
typedef long long ll;
using namespace std;
int nt[1000006],f[1000006],raw[1000005];
int compress(int x){
	int cur;int pd = 1,dcur;
	while (x > 1){
		cur = nt[x];
		dcur = 0;
		while (nt[x] == cur && x > 1){
			x /= nt[x];
			dcur++;
		}
		if (dcur % 2 == 1) pd *= cur;
	}
	return pd;
}
int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(0);cout.tie(0);
   memset(nt,0,sizeof(nt));
   for (int i = 2 ; i <= 1000000 ; i++)
     if (nt[i] == 0){
       	nt[i] = i;
       	for (int j = i ; j <= 1000000/i ; j++)
    	  if (nt[i*j] == 0) nt[i*j] = i;
    }
   int x,y,t,n,q,m,res1,res2;ll w;
   cin >> t;
   while (t--){
   	cin >> n;m = 0;res1 = 0;res2 = 0;
   	for (int i = 1 ; i <= n ; i++){
   		cin >> x;
   		y = compress(x);
   		if (f[y] > 0) f[y]++;
		 else{
		    f[y] = 1;
			m++;raw[m] = y; 	
		   } 
	   }
	   if (f[1] % 2 == 1) res1 = f[1];
	 for (int i = 1 ; i <= m ; i++){
		res2 = max(res2,f[raw[i]]);
	 	if (f[raw[i]] % 2 == 0) res1 += f[raw[i]];
	 	f[raw[i]] = 0;
	 }   
	 res1 = max(res1,res2);
	 cin >> q;
	 while (q--){
	 	cin >> w;
	 	if (w >= 1) cout << res1;
		 else cout << res2;
		cout << endl; 
	 }   
   } 
   return 0;
}


Comments

Submit
0 Comments
More Questions

811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort