UVa10484 - Divisibility of Factors(数论)
2014-07-08 16:35
323 查看
Given two integers N and D, you will have to find how many of the factors of N! (factorial N) are divisible by D.
Input
The input file contains several lines of input. Each line contains two integers N (0<=N<= 100) and D(0<|D|<=2^31-1). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains a single integer, which denotes of many different factors of N! are divisible byD.
Sample Input
10 2
9 3
0 0
Sample Output
240
128
主要用到素数筛选法,求出50000内的素数
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
using namespace std;
const int N = 50000;
vector<int> vPrime;
bool vis
;
int n, d;
void calc(int n, map<int, int>& m);
void sieve_of_sundaram();
bool input();
void solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif
sieve_of_sundaram();
while (input()) {
solve();
}
return 0;
}
bool input()
{
scanf("%d%d", &n, &d);
if (n == 0 && d == 0) return false;
d = abs(d);
return true;
}
void solve()
{
if (n == 0 && d == 1) {
printf("1\n");
return;
}
map<int, int> nmap, dmap;
for (int i = 2; i <= n; i++) calc(i, nmap);
calc(d, dmap);
for (map<int, int>::iterator it = dmap.begin(); it != dmap.end(); it++) {
if (nmap.count(it->first) == 0) {
printf("0\n");
return;
}
if (nmap[it->first] < it->second) {
printf("0\n");
return;
}
nmap[it->first] -= it->second;
}
long long ans = 1;
for (map<int, int>::iterator it = nmap.begin(); it != nmap.end(); it++) {
ans *= (nmap[it->first] + 1);
}
printf("%lld\n", ans);
}
void sieve_of_sundaram()
{
memset(vis, false, sizeof(vis));
int m = (int)sqrt(N / 2);
for (int i = 1; i < m; i++) {
if (vis[i]) continue;
for (int k = 2 * i + 1, j = 2 * i * (i + 1); j < N; j += k) {
vis[j] = true;
}
}
vPrime.push_back(2);
for (int i = 1; i < N / 2; i++) {
if (!vis[i]) vPrime.push_back(2 * i + 1);
}
}
void calc(int n, map<int, int>& m)
{
for (size_t i = 0; i < vPrime.size(); i++) {
int cnt = 0;
if (n < vPrime[i]) break;
if (n % vPrime[i] == 0) {
while (n % vPrime[i] == 0) {
n /= vPrime[i];
cnt++;
}
m[vPrime[i]] += cnt;
}
}
if (n != 1) m
+= 1;
}
Input
The input file contains several lines of input. Each line contains two integers N (0<=N<= 100) and D(0<|D|<=2^31-1). Input is terminated by a line containing two zeroes. This line should not be processed.
Output
For each line of input produce one line of output. This line contains a single integer, which denotes of many different factors of N! are divisible byD.
Sample Input
10 2
9 3
0 0
Sample Output
240
128
主要用到素数筛选法,求出50000内的素数
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
using namespace std;
const int N = 50000;
vector<int> vPrime;
bool vis
;
int n, d;
void calc(int n, map<int, int>& m);
void sieve_of_sundaram();
bool input();
void solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("e:\\uva_in.txt", "r", stdin);
#endif
sieve_of_sundaram();
while (input()) {
solve();
}
return 0;
}
bool input()
{
scanf("%d%d", &n, &d);
if (n == 0 && d == 0) return false;
d = abs(d);
return true;
}
void solve()
{
if (n == 0 && d == 1) {
printf("1\n");
return;
}
map<int, int> nmap, dmap;
for (int i = 2; i <= n; i++) calc(i, nmap);
calc(d, dmap);
for (map<int, int>::iterator it = dmap.begin(); it != dmap.end(); it++) {
if (nmap.count(it->first) == 0) {
printf("0\n");
return;
}
if (nmap[it->first] < it->second) {
printf("0\n");
return;
}
nmap[it->first] -= it->second;
}
long long ans = 1;
for (map<int, int>::iterator it = nmap.begin(); it != nmap.end(); it++) {
ans *= (nmap[it->first] + 1);
}
printf("%lld\n", ans);
}
void sieve_of_sundaram()
{
memset(vis, false, sizeof(vis));
int m = (int)sqrt(N / 2);
for (int i = 1; i < m; i++) {
if (vis[i]) continue;
for (int k = 2 * i + 1, j = 2 * i * (i + 1); j < N; j += k) {
vis[j] = true;
}
}
vPrime.push_back(2);
for (int i = 1; i < N / 2; i++) {
if (!vis[i]) vPrime.push_back(2 * i + 1);
}
}
void calc(int n, map<int, int>& m)
{
for (size_t i = 0; i < vPrime.size(); i++) {
int cnt = 0;
if (n < vPrime[i]) break;
if (n % vPrime[i] == 0) {
while (n % vPrime[i] == 0) {
n /= vPrime[i];
cnt++;
}
m[vPrime[i]] += cnt;
}
}
if (n != 1) m
+= 1;
}
相关文章推荐
- UVA 10168 Summation of Four Primes(数论)
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVa 160 Factors and Factorials (数论)
- UVa 160 Factors ans Factorials(数论)
- UVA 10168 Summation of Four Primes(数论)
- UVA 1210 Sum of Consecutive Prime Numbers(数论)
- 数论刷题-uva【10168】- Summation of Four Primes
- UVa 993 Product of digits(简单数论)
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- (数论2.1.2)UVA 10168 Summation of Four Primes(欧拉筛法)
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- Uva 10820 Send a Table(数论、欧拉筛法)
- UVA 11610 Reverse Prime (数论+树状数组+二分,难题)
- UVA 10090 - Marbles (数论)
- uva1406 - A Sequence of Numbers 树状数组
- UVA - 11292 Dragon of Loowater
- uva 10285 The Tower of Babylon(dp,记忆化搜索)
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA10519 - !! Really Strange !!(数论+高精度)
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers