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;
}
目录
相关文章
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
152 0
HDU-1097,A hard puzzle(快速幂)
HDU-1097,A hard puzzle(快速幂)
HDU-1017,A Mathematical Curiosity
HDU-1017,A Mathematical Curiosity
|
人工智能 BI 机器学习/深度学习
|
人工智能
UVA 11039-Building designing【贪心+绝对值排序】
UVA11039-Building designing Time limit: 3.000 seconds An architect wants to design a very high building.
1184 0