UVa11876 - N + NOD (N)
来源:互联网 发布:游戏支付软件 编辑:程序博客网 时间:2024/05/02 04:44
I
N + NOD (N)
Input
Standard Input
Output
Standard Output
Consider an integer sequenceN where,
N0 = 1
Ni = Ni-1 + NOD(Ni-1)- for i > 0
Here, NOD(x) =number of divisors of x.
So the first few terms of this sequence are 1 2 4 7 9 12 18…
Given two integers A and B, find out the number ofintegers in the above sequence that lies within the range [A, B].
Input
The first line of input is an integer T(T < 100000), thatindicates the number of test cases. Each case contains two integers,A followed by B (1 ≤ A ≤ B ≤ 1000000).
Output
For each case, output the case numberfirst followed by the required result.
Sample Input
Sample Output
3
1 18
1 100
3000 4000
Case 1: 7
Case 2: 20
Case 3: 87
#include <cstdio>#include <cstring>#include <map>#include <cmath>#include <algorithm>using namespace std;const int N = 1100;const int M = 65000;;const int P = 200;const int MAX = 1000001;bool vis[N];int a, b;int vPrime[P], primeCnt;int Ni[M], NiCnt;int ans[MAX];void input();int solve();void sieve_of_sundaram();int cal(int n);void init();int main(){#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);#endifinit();int t;scanf("%d", &t);for (int i = 1; i <= t; i++) {input();printf("Case %d: %d\n", i, solve());}return 0;}void sieve_of_sundaram(){int m = (int)sqrt(N / 2);memset(vis, false, sizeof(vis));for (int i = 1; i < m; i++) {if (vis[i]) continue;for (int j = 2 * i * (i + 1), k = 2 * i + 1; j < N; j += k) {vis[j] = true;}}primeCnt = 0;vPrime[primeCnt++] = 2;for (int i = 1; i < N / 2; i++) {if (!vis[i]) vPrime[primeCnt++] = 2 * i + 1;}}int cal(int n){map<int, int> m;for (int i = 0; i < primeCnt; i++) {if (n < vPrime[i]) break;if (n % vPrime[i] == 0) {int cnt = 0;while (n % vPrime[i] == 0) {cnt++; n /= vPrime[i];}m[vPrime[i]] = cnt;}}if (n != 1) m[n] = 1;int ans = 1;for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {ans *= (it->second + 1);}return ans;}void init(){sieve_of_sundaram();NiCnt = 0;Ni[NiCnt++] = 1;ans[0] = 0;ans[1] = 1;while (true) {int n = Ni[NiCnt - 1];int m = n + cal(n);if (m > 1000000) {fill(&ans[n], &ans[1000001], NiCnt);break;}fill(&ans[n], &ans[m], NiCnt);Ni[NiCnt++] = m;}}void input(){scanf("%d%d", &a, &b);}int solve(){return ans[b] - ans[a - 1];}
0 0
- UVa11876 - N + NOD (N)
- UVa 11876 - N + NOD (N)
- 51NOD N!
- 51nod 1004 n^n问题
- 51nod N的阶乘
- 51nod N的阶乘
- 51Nod-1582-n叉树
- 51nod 1004 n^n的末尾数字
- 51nod 1004 n^n的末位数字
- 51nod-【1004 n^n的末位数字】
- 51nod 1004 n^n的末位数字
- 51nod 1004 n^n的末位数字
- 51NOD 1004 n^n的末位数字
- [51NOD] 1004 n^n的末位数字 [数学]
- 51nod 1004 n^n的末位数字
- 51Nod 1004 n^n的末位数字
- 【51Nod】1004 n^n的末位数字
- 51Nod--1004 n^n的末位数字
- Struts——三大组件(一)ActionServlet
- 最大的幻术-游戏开发-我的游戏构思-环境
- 快速排序
- sql语句分别按日,按周,按月,按季统计金额
- servlet和cgi的区别
- UVa11876 - N + NOD (N)
- Eclipse背景颜色,字体大小,自定义格式化设置,自定义keys,自动注释,各大src源码下载,自定义注释
- AIX下RAC搭建 Oracle10G(四)安装CRS
- ArrayList,LinkedList,Vestor 区别
- Swift常量和变量
- 二叉树
- JVM性能调优
- 最大公因数、最小公倍数、因式分解
- Struts——三大组件(二)RequestProcessor