답안 #844515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844515 2023-09-05T13:46:21 Z vjudge1 Spiderman (COCI20_spiderman) C++11
28 / 70
2000 ms 8068 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;
		}
		if(pfact.size()==0 and i*i>n){
			pfact.pb(mp(n, 1));
			break;
		}
	}
	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 122 ms 4436 KB Output is correct
2 Correct 78 ms 4184 KB Output is correct
3 Correct 197 ms 5496 KB Output is correct
4 Correct 265 ms 7552 KB Output is correct
5 Execution timed out 2055 ms 6416 KB Time limit exceeded
6 Execution timed out 2057 ms 8020 KB Time limit exceeded
7 Execution timed out 2044 ms 6572 KB Time limit exceeded
8 Execution timed out 2072 ms 6320 KB Time limit exceeded
9 Execution timed out 2066 ms 8068 KB Time limit exceeded
10 Execution timed out 2059 ms 7880 KB Time limit exceeded