Islamic Universtiy,Bangladesh TopH 9th CPU CSE Programming Contest C. Birth Day Gift Solution
Problem Link : https://toph.co/arena?practice=647629a9d47a320767bffe27#!/p/6474e251d47a320767bfdfce
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- const int mx = 2e7 + 3;
- using ll = long long;
- ll vis[mx], prime[mx], k;
- void sieve() {
- for (int i = 3; i <= mx; i += 2) {
- if (vis[i] == 0) {
- for (int j = 2 * i; j <= mx; j += i) {
- vis[j] = 1;
- }
- }
- }
- vis[0] = vis[1] = 1;
- prime[0] = 2;
- k = 1;
- for (int i = 3; i <= mx; i += 2) {
- if (!vis[i]) {
- prime[k] = i;
- k++;
- }
- }
- }
- ll NOD(ll n) {
- int ans = 1;
- for (int i = 0; i < k and prime[i] * prime[i] <= n; i++) {
- if (n % prime[i] == 0) {
- int cnt = 0;
- while (n % prime[i] == 0) {
- n /= prime[i];
- cnt++;
- }
- cnt++;
- ans *= cnt;
- }
- }
- if (n != 1) {
- ans *= 2;
- }
- return ans;
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- sieve();
- ll t;
- cin >> t;
- while (t--) {
- ll n;
- cin >> n;
- cout << NOD(n) << '\n';
- }
- return 0;
- }
No comments