Submission #844544

# Submission time Handle Problem Language Result Execution time Memory
844544 2023-09-05T14:01:28 Z vjudge1 Spiderman (COCI20_spiderman) C++11
70 / 70
331 ms 11824 KB
#include <bits/stdc++.h>
#define lg(a) (31 - __builtin_clz((a)))
#define endl ("\n")
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(aa) aa.begin(), aa.end()
#define forn(i, n) for(int i=0;i<n;i++)
#define trav(e, x) for(auto& e:x)
#define until(n, v) (int) (lower_bound(v.begin(), v.end(), n)-v.begin()) //# of elements < n
#define after(n, v) (int) (v.end()-upper_bound(v.begin(), v.end(), n))   //# of elements > n
#define sameas(n, v) (int) (upper_bound(v.begin(), v.end(), n) - lower_bound(v.begin(), v.end(), n)) //# of elements ==n
typedef long long ll;

using namespace std;
/*

*/

vector<int> genfac(vector<pair<int, int>> pfact, int mpindex, vector<int> factors){
	if (mpindex == pfact.size()-1) return factors;
	mpindex++;
	int n = factors.size();
	int exp = 1;
	for(int i=1;i<=pfact[mpindex].second;i++){
		exp*=pfact[mpindex].first;
		for(int j=0;j<n;j++){
			factors.pb(exp * factors[j]);
		}
	}
	return genfac(pfact, mpindex, factors);
}

vector<int> factorize(int n){
	vector<pair<int, int>> pfact;
	if(n%2==0){
		n/=2;
		pfact.pb(mp(2, 1));
	}
	while(n%2==0){
		pfact.back().second++;
		n/=2;
	}
	for(int i=3;i*i<=n;i+=2){
		if(n%i==0){
			n/=i;
			pfact.pb(mp(i, 1));
		}
		while(n%i==0){
			pfact.back().second++;
			n/=i;
		}
	}
	vector<int> factors; factors.pb(1);
	if(n!=1) factors.pb(n);
	factors = genfac(pfact, -1, factors);
	return factors;
}




void solve(){
	int n, k; cin>>n>>k;
	vi v(n), cnt(1e6+5, 0);
	for(auto &e:v){
		cin>>e; cnt[e]++;
	}
	map<int, int> mp;
	vi sorted(v);
	sort(all(sorted));
	for(int i=0; i<n;i++){
		int ans = 0;
		if(mp.count(v[i])){
			cout<<mp[v[i]]<<' ';
			continue;
		}
		if(v[i] >= k){
			if(v[i] == k){
				ans = after(k, sorted);
			}
			else{
				vi p(factorize(abs(v[i]-k)));
				for(auto e : p){
					if(e > k) ans+= cnt[e] - (e==v[i]);
				}
			}
		}
		mp[v[i]] = ans;
		cout<<ans<<' ';
	}

}

int main(){
	solve();
}



Compilation message

spiderman.cpp: In function 'std::vector<int> genfac(std::vector<std::pair<int, int> >, int, std::vector<int>)':
spiderman.cpp:23:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  if (mpindex == pfact.size()-1) return factors;
      |      ~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 4 ms 4404 KB Output is correct
3 Correct 41 ms 5456 KB Output is correct
4 Correct 115 ms 7760 KB Output is correct
5 Correct 176 ms 10320 KB Output is correct
6 Correct 295 ms 11720 KB Output is correct
7 Correct 204 ms 9968 KB Output is correct
8 Correct 195 ms 9956 KB Output is correct
9 Correct 325 ms 11824 KB Output is correct
10 Correct 331 ms 11672 KB Output is correct