UVa 10139 Factovisors
来源:互联网 发布:里基戴维斯数据 编辑:程序博客网 时间:2024/05/11 16:02
这道题同样的代码用 JAVA 会超时,用 C++ 才通过。
#include <iostream>#include <vector>#include <cmath>using namespace std;// 素数筛的数组长度long LEN = 1 << 16;// 用于保存筛出的素数vector<long> primes;// 利用筛法求 2^16 以内的素数,保存到 primes 全局变量void init() {bool filter[LEN];for ( int i=0; i<LEN; i++ ) filter[i] = true;long max = (long)sqrt(LEN)+1;for ( int i=2; i<=max; i++ ) {if ( filter[i] ) {for ( int j=2*i; j<LEN; j+=i )filter[j] = false;}}for ( int i=2; i<LEN; i++ )if ( filter[i] ) {primes.push_back(i);}}// 计算素数 p ^ k 是否整除 n!bool divByP2Kth(long p, long k, long n) {if ( k==0 ) return true;long cnt = 0;for ( long i=p; i<=n; i*=p ) {cnt += n/i;if ( cnt >= k ) return true;}return false;}// 计算 m 是否整除 n!bool divide(long m, long n) {if ( m==0 )return false;else if ( m== 1 )return true;for ( unsigned int i=0; i<primes.size(); i++ ) {// 求 m 的素因数分解中素数 p 的指数 klong p = primes.at(i), k = 0;while ( m%p == 0 ) {k++; m /= p;}// p^k 是否整除 n! ?if ( ! divByP2Kth(p,k,n) ) return false;// 分解完毕if ( m==1 ) break;}// 如果剩余一个大于 2^16 的素因子if ( m>1 && ! divByP2Kth(m,1,n) ) return false;return true;}int main() {init();long n, M;while (cin >> n >> M) {bool ok = divide(M,n);if ( ok ) {cout << M << " divides " << n << "!" << endl;} else {cout << M << " does not divide " << n << "!" << endl;}}return 0;}
- UVa 10139 Factovisors
- uva 10139 factovisors
- Uva 10139 Factovisors
- UVa 10139 Factovisors
- UVa 10139 - Factovisors
- UVa 10139 - Factovisors
- UVA 10139 Factovisors
- UVa Problem Solution: 10139 - Factovisors
- UVA 10139 Factovisors(数论)
- 【 UVa 10139】 Factovisors 【 m | n!? 】
- PC/UVa 题号: 110704/10139 Factovisors
- UVa 10139 Factovisors (阶乘能否整除?)
- UVa-10139 Factovisors -(阶乘的整除)
- UVa Problem 10139 Factovisors (阶乘与整除)
- Factovisors-ACM
- JOJ1926:Factovisors
- 110704 Factovisors
- Factovisors - PC110704
- 软件工程之八 软件维护
- 关于kindeditor在tomcat和apache下运行的问题
- SQLite剖析(5):体系结构
- xcode4的自动完成功能(Code sense or Code Snippet)
- 软件工程之九 质量管理
- UVa 10139 Factovisors
- 在Eclipse中关联源代码
- SQLite剖析(6):临时文件和内存数据库
- openssl加密原理
- BlackBerry 10 NDK中如何打印调试信息
- MATLAB 3D 动画简单实例
- 别忘了虚析构
- 新书《深入理解计算机网络》可正式购买了
- 关于移动产品趋势的一些见解,大而全还是小而精?