1831D - The BOSS Can Count Pairs - CodeForces Solution


brute force data structures math

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
using namespace std;
const int N=2e5+5;
const ll md=1e9+7;
const ll inf=1e18;
vector<ll>v[N*2];
ll a[N],b[N],n,f[N*2];
void solve(){
	//fuck
	cin>>n;
	for(int i=1;i<=2*n;i++){
		v[i].clear();		
	}
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
		v[a[i]].push_back(b[i]);
	}
	ll ans=0;
	for(int x=1;x*x<=2*n;x++){
		for(int now:v[x]){
			ans+=(x*x-now>0?f[x*x-now]:0ll);
			f[now]++;
		}
		for(int y=x+1;y*x<=2*n;y++){
			for(int now:v[y]){
				ans+=(y*x-now>0?f[x*y-now]:0ll);
			}
		}
		for(int now:v[x]){
			f[now]--;
		}
	}
	cout<<ans<<endl;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	cin>>t;
	memset(f,0,sizeof(f));
	while(t--){
		solve();
	}
	
}


Comments

Submit
0 Comments
More Questions

1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification