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];
}
目录
相关文章
|
7月前
uva 11991 - Easy Problem from Rujia Liu?
这个题目的意思是输入n个数,m组询问,每组询问包含两个整数k,v,意思是询问整数v第k次出现的位置。
29 0
|
9月前
UVa1531 - Problem Bee
UVa1531 - Problem Bee
35 0
|
9月前
uva127 "Accordian" Patience
uva127 "Accordian" Patience
25 0
UVA10474 大理石在哪儿 Where is the Marble?
UVA10474 大理石在哪儿 Where is the Marble?
UVA - 10474 Where is the Marble
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them.
1368 0
|
C++
uva 11991 Easy Problem from Rujia Liu?
点击打开链接uva 11991 思路: STL 分析: 1 题目要求的是第k个v的下标 2 题目的规模是10^6如果用暴力的话那么超时是肯定的,所以这里应该考虑用vector数组,每一个值作为一个vector,,然后把这个值出现在第几个位...
829 0
uva 100 The 3n+1 problem
题目链接: http://www.programming-challenges.com/pg.php?page=studenthome /* The 3n+1 problem 计算每个数的循环节长度,求给定区间的循环节长度的最大值。 */ #include&lt;iostream&gt; #include&lt;stdio.h&gt; using namespace std;
1151 0
uva 11624 - Fire!
点击打开链接uva 11624 思路:bfs 分析: 1 题目要判断joe是否可以逃出迷宫,如果可以输出最小的时间,否则输出impossible 2 题目明确规定有且仅有一个Joe,但是火的个数是不确定的 3 那么如果没有火,我们只要去求Joe走出迷宫的时间即可。
1017 0