UVa11876 - N + NOD (N)

简介: UVa11876 - N + NOD (N)
#include <cstdio>#include <cstring>#include <map>#include <cmath>#include <algorithm>usingnamespacestd;
constintN=1100;
constintM=65000;;
constintP=200;
constintMAX=1000001;
boolvis[N];
inta, b;
intvPrime[P], primeCnt;
intNi[M], NiCnt;
intans[MAX];
voidinput();
intsolve();
voidsieve_of_sundaram();
intcal(intn);
voidinit();
intmain()
{
#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endifinit();
intt;
scanf("%d", &t);
for (inti=1; i<=t; i++) {
input();
printf("Case %d: %d\n", i, solve());
    }
return0;
}
voidsieve_of_sundaram()
{
intm= (int)sqrt(N/2);
memset(vis, false, sizeof(vis));
for (inti=1; i<m; i++) {
if (vis[i]) continue;
for (intj=2*i* (i+1), k=2*i+1; j<N; j+=k) {
vis[j] =true;
        }
    }
primeCnt=0;
vPrime[primeCnt++] =2;
for (inti=1; i<N/2; i++) {
if (!vis[i]) vPrime[primeCnt++] =2*i+1;
    }
}
intcal(intn)
{
map<int, int>m;
for (inti=0; i<primeCnt; i++) {
if (n<vPrime[i]) break;
if (n%vPrime[i] ==0) {
intcnt=0;
while (n%vPrime[i] ==0) {
cnt++; 
n/=vPrime[i];
            }
m[vPrime[i]] =cnt;
        }
    }
if (n!=1) m[n] =1;
intans=1;
for (map<int, int>::iteratorit=m.begin(); it!=m.end(); it++) {
ans*= (it->second+1);
    }
returnans;
}
voidinit()
{
sieve_of_sundaram();
NiCnt=0;
Ni[NiCnt++] =1;
ans[0] =0;
ans[1] =1;
while (true) {
intn=Ni[NiCnt-1];
intm=n+cal(n);
if (m>1000000) {
fill(&ans[n], &ans[1000001], NiCnt);
break;
        }
fill(&ans[n], &ans[m], NiCnt);
Ni[NiCnt++] =m;
    }
}
voidinput()
{
scanf("%d%d", &a, &b);
}
intsolve()
{
returnans[b] -ans[a-1];
}
目录
相关文章
uva10038 Jolly Jumpers
uva10038 Jolly Jumpers
46 0
uva127 "Accordian" Patience
uva127 "Accordian" Patience
47 0
uva10152 ShellSort
uva10152 ShellSort
66 0
UVa11776 - Oh Your Royal Greediness!
UVa11776 - Oh Your Royal Greediness!
58 0
uva375 Inscribed Circles and Isosceles Triangles
uva375 Inscribed Circles and Isosceles Triangles
45 0
UVa 10082 WERTYU
UVa 10082 WERTYU
132 0
|
存储 固态存储
|
机器学习/深度学习
uva 12470 Tribonacci
点击打开uva12470  思路: 矩阵快速幂 分析: 1 裸题 代码: /************************************************ * By: chenguolin ...
996 0
|
C++
uva 11136 Hoax or what
点击打开链接uva 11136 思路: STL 分析: 1 题目意思比较不好理解,理解了题目之后我们可以利用STL的multiset来做 2 每次找到最大和最小的值,然后求解即可 代码: #include #include #in...
849 0

热门文章

最新文章

下一篇
开通oss服务