UVa10484 - Divisibility of Factors(数论)
来源:互联网 发布:c语言 函数库啥意思- 编辑:程序博客网 时间:2024/05/20 19:45
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[N];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[n] += 1;}
0 0
- UVa10484 - Divisibility of Factors(数论)
- Factors of Project success
- CodeForces 630 J. Divisibility(数论)
- UVa 160 Factors ans Factorials(数论)
- UVa 160 Factors and Factorials (数论)
- Divisibility
- Divisibility
- Divisibility
- Divisibility
- Divisibility
- Divisibility
- Divisibility
- fzu 1912 Divisibility by Thirty-six(数论+枚举)
- Find all prime factors of a number
- Find factors' count of a Number
- List of some journal impact factors
- CC NUMFACT(Number of Factors-枚举因子)
- All factors of a given number
- SQLite数据库增删改查操作
- ZigBee EndPoint理解
- 如何在页面中嵌入plugin对象(兼容不同浏览器)
- 【Python】Python编程规范(PEP8)阅读摘要
- InstallShield 2014正式发布
- UVa10484 - Divisibility of Factors(数论)
- oracle精确计算出两个时间的间隔
- 国嵌深入班-PCI
- java正则表达式
- 输出一个集合的所有子集合-Java代码实现(二)
- genalloc — 通用内存分配器
- out of memory
- Travelling - HDU 3001 状压dp
- GetMemory错误讲解(指针练习)