QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#790048#9531. Weird CeilingJuliano1RE 5ms4752kbC++142.1kb2024-11-28 00:02:202024-11-28 00:02:25

Judging History

你现在查看的是最新测评结果

  • [2024-11-28 00:02:25]
  • 评测
  • 测评结果:RE
  • 用时:5ms
  • 内存:4752kb
  • [2024-11-28 00:02:20]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

using pii = pair<int, int>;

const int maxn = 1e6;
bool NotPrime[maxn + 5];
vector<int> Prime;

bool isPrime(int x) {
	if (x < maxn)return (!NotPrime[x]);
	if (x == 1 || x == 0)return 0;
	int R = sqrt(x) + 1;
	for (int i = 2; i <= R; i++) {
		if (x % i == 0)return 0;
	}
	return 1;
}

void ini() {
	NotPrime[0] = 1;
	NotPrime[1] = 1;
	for (int i = 2; i < maxn; i++) {
		if (NotPrime[i])continue;
		Prime.push_back(i);
		for (int j = 2; i * j < maxn; j++) {
			NotPrime[i * j] = 1;
		}
	}
}

// 质因子分解
vector<pii> solve(int x) {
	map<int, int> cnt;
	for (int i = 0; i < Prime.size(); i++) {
		while (x % Prime[i] == 0) {
			cnt[Prime[i]]++;
			x /= Prime[i];
		}
		if (x > maxn && isPrime(x)) { cnt[x]++; break; }
	}
	
	vector<pii> ans;
	for (auto it : cnt) 
		ans.push_back({ it.first,it.second });
	
	return ans;
}

void get_factor(vector<pii> cnt, set<int>& ans, int idx, int val) {
	if (idx == cnt.size())return;
	// 质因数 now 有 sum 个
	int now = cnt[idx].first;
	int sum = cnt[idx].second;

	int pwr = 1;
	for (int i = 0; i <= sum; i++) {
		val *= pwr;
		ans.insert(val);
		get_factor(cnt, ans, idx+1, val);
		val /= pwr;
		pwr *= now;
	}
}

int func(int a, int b,set<int> &st) {
	auto it = st.upper_bound(b); it--;
	return (int)(a / (*it));
}

int bino(int L0, int n,set<int> st) {
	int L = L0 - 1, R = n + 1;
	int val = func(n, L0,st);
	while (L + 1 != R) {
		int mid = (L + R) / 2;
		int now = func(n, mid,st);
		if (now == val) L = mid;
		else R = mid;
	}
	return L;
}


signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	ini();
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		auto cnt = solve(n);
		set<int> st;
		get_factor(cnt, st, 0, 1);
		int sum = 0;
		int L = 1;
		while (L <= n) {
			int R = bino(L, n,st); // 值为 func(n,L) 的区间
			sum += (R - L + 1) * func(n, L,st);
			L = R + 1;
		}
		cout << sum << endl;
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 4752kb

input:

3
5
451
114514

output:

21
10251
7075858

result:

ok 3 lines

Test #2:

score: -100
Runtime Error

input:

1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101...

output:


result: