UVa10484 - Divisibility of Factors(数论)

简介: UVa10484 - Divisibility of Factors(数论)
#include <cstdio>#include <vector>#include <cstring>#include <cmath>#include <map>#include <cstdlib>usingnamespacestd;
constintN=50000;
vector<int>vPrime;
boolvis[N];
intn, d;
voidcalc(intn, map<int, int>&m);
voidsieve_of_sundaram();
boolinput();
voidsolve();
intmain()
{
#ifndef ONLINE_JUDGEfreopen("e:\\uva_in.txt", "r", stdin);
#endifsieve_of_sundaram();
while (input()) {
solve();
    }
return0;
}
boolinput()
{
scanf("%d%d", &n, &d);
if (n==0&&d==0) returnfalse;
d=abs(d);
returntrue;
}
voidsolve()
{
if (n==0&&d==1) {
printf("1\n");
return;
    }
map<int, int>nmap, dmap;
for (inti=2; i<=n; i++) calc(i, nmap);
calc(d, dmap);
for (map<int, int>::iteratorit=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;
    }
longlongans=1;
for (map<int, int>::iteratorit=nmap.begin(); it!=nmap.end(); it++) {
ans*= (nmap[it->first] +1);
    }
printf("%lld\n", ans);
}
voidsieve_of_sundaram()
{
memset(vis, false, sizeof(vis));
intm= (int)sqrt(N/2);
for (inti=1; i<m; i++) {
if (vis[i]) continue;
for (intk=2*i+1, j=2*i* (i+1); j<N; j+=k) {
vis[j] =true;
        }
    }
vPrime.push_back(2);
for (inti=1; i<N/2; i++) {
if (!vis[i]) vPrime.push_back(2*i+1);
    }
}
voidcalc(intn, map<int, int>&m)
{
for (size_ti=0; i<vPrime.size(); i++) {
intcnt=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;
}
目录
相关文章
|
人工智能 算法 BI
三对角线性方程组(tridiagonal systems of equations)的求解
三对角线性方程组(tridiagonal systems of equations)   三对角线性方程组,对于熟悉数值分析的同学来说,并不陌生,它经常出现在微分方程的数值求解和三次样条函数的插值问题中。
1754 0
|
人工智能 BI 机器学习/深度学习
|
机器学习/深度学习 算法
数论 + 扩展欧几里得 - SGU 106. The equation
 The equation  Problem's Link   Mean:  给你7个数,a,b,c,x1,x2,y1,y2.求满足a*x+b*y=-c的解x满足x1= lx)    {        ans = min(rx,ry)-ma...
1187 0
概率论 --- Uva 11181 Probability|Given
Uva 11181 Probability|Given  Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18546   Mean:  n个人去逛超市,第i个人会购买东西的概率是Pi。
1035 0
|
算法
poj 3352Road Construction(无向双连通分量的分解)
1 /* 2 题意:给定一个连通的无向图G,至少要添加几条边,才能使其变为强连通图(指的是边强联通)。 3 思路:利用tarjan算法找出所有的双联通分量!然后根据low[]值的不同将双联通分量 4 进行缩点,最后图形会变成一棵树!也就是添加至少多少条边使一棵树...
766 0
|
机器学习/深度学习
Factors 分解质因数
package com.yourself.yours; import java.util.Scanner; /** *************************************************************** * @author cc Factors * 分解质因数 如: 输入90 打印 90=2*3*3*5 * 分析: 对
1191 0