답안 #844511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844511 2023-09-05T13:44:44 Z vjudge1 Spiderman (COCI20_spiderman) C++11
28 / 70
2000 ms 7708 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;
	for(int i=2;i<=n;i++){
		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);
	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);
			}
			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;
      |      ~~~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 4436 KB Output is correct
2 Correct 145 ms 4404 KB Output is correct
3 Correct 319 ms 5320 KB Output is correct
4 Correct 389 ms 7708 KB Output is correct
5 Execution timed out 2055 ms 5904 KB Time limit exceeded
6 Execution timed out 2055 ms 7356 KB Time limit exceeded
7 Execution timed out 2054 ms 5964 KB Time limit exceeded
8 Execution timed out 2036 ms 6088 KB Time limit exceeded
9 Execution timed out 2029 ms 7152 KB Time limit exceeded
10 Execution timed out 2045 ms 7260 KB Time limit exceeded