Yuhao and William are both great friends. One day, Yuhao told William about prime numbers but William got annoyed after knowing that prime numbers do not have any divisor except 1 and itself. William only likes non-prime numbers. To appease William, Yuhao decided to give William a problem:
Given a number , find the number of its non-prime divisors.
Example:
Input: n = 4
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){int N = 100000, prime[100001] = {0}, v[100001];prime[2] = 1;for (long long int i = 3; i <= N; i += 2)prime[i] = 1;v[0] = 2;long long int k = 1;for (long long int i = 3; i <= N; i += 2){if (prime[i]){v[k] = i;k++;for (long long int j = i * i; j <= N; j += 2 * i)prime[j] = 0;}}long long int n = 4;long long int i = 0, p1 = v[0], ans = 1, d = 0;while (p1 * p1 <= n && i < k){long long int c = 0;while (n % p1 == 0){n = n / p1;c++;}if (c > 0)d++;ans *= (c + 1);i++;p1 = v[i];}if (n != 1){ans *= 2;ans -= 1;}cout << ans - d << endl;return 0;}
No comments:
Post a Comment