UVA12043 Divisors【欧拉筛法】
Let us define the functions d(n) and σ(n) as
d(n)=numberofdivisorsofnd(n) = number of divisors of nd(n)=numberofdivisorsofn
σ(n)=summationofdivisorsofnσ(n) = summation of divisors of nσ(n)=summationofdivisorsofn
Here divisors of n include both 1 and n. For example divisors of 6 are 1, 2, 3 and 6. So d(6) = 4 and σ(n) = 12.
Now let us define two more function g(a, b, k) and h(a, b, k) as
g(a,b,k)=∑id(i)g(a, b, k) = \sum_i d(i)g(a,b,k)=i∑d(i)
h(a,b,k)=∑iσ(i)h(a, b, k) = \sum_i σ(i)h(a,b,k)=i∑σ(i)
Where a ≤ i ≤ b and i is divisible by k.
For example, g(5, 12, 3) = d(6)+d(9)+d(12) = 4+3+6 = 13 and h(5, 12, 3) = σ(6)+σ(9)+σ(12) = 13 + 13 + 28 = 53. Given a, b, k you have to calculate g(a, b, k) and h(a, b, k).
Input
The first line of the input file contains an integer T (T ≤ 75) which denotes the total number of test cases. The description of each test case is given below:
Three integers in a line. First integer is contains a, second integer is b and third integer is k. You may assume 0 < a ≤ b ≤ 105, 0 < k < 2000.
Output
For each test case print one line containing g(a, b, k) and h(a, b, k) separated by a space as defined above.
Sample Input
2
5 12 3
1 100 3
Sample Output
13 53
217 3323
问题链接:UVA12043 Divisors
问题简述:(略)
问题分析:数论筛法问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA12043 Divisors */#include <bits/stdc++.h>using namespace std;// 欧拉筛法
const int N = 400;
bool isprime[N + 1];
int prime[N / 3], pcnt = 0;
void eulersieve(void)
{memset(isprime, true, sizeof(isprime));isprime[0] = isprime[1] = false;for(int i = 2; i <= N; i++) {if(isprime[i])prime[pcnt++] = i;for(int j = 0; j < pcnt && i * prime[j] <= N; j++) { //筛选isprime[i * prime[j]] = false;if(i % prime[j] == 0) break;}}
}long long nod(int n)
{long long ret = 1;for (int i = 0; prime[i] * prime[i] <= n; i++){int j;for (j = 0; n % prime[i] == 0; j++)n /= prime[i];ret *= j + 1;}if (n > 1) ret <<= 1;return ret;
}long long sod(int n)
{long long ret = 1;for (int i = 0; prime[i] * prime[i] <= n; i++){long long p = 1;int j;for (j = 0; n % prime[i] == 0; j++) {n /= prime[i];p *= prime[i];}ret *= (p * prime[i] - 1) / (prime[i] - 1);}if (n > 1) ret *= n + 1;return ret;
}int main()
{eulersieve();int t, a, b, k;scanf("%d", &t);while (t--) {scanf("%d%d%d", &a, &b, &k);long long sum_nod = 0, sum_sod = 0;a = (a + k - 1) / k * k;for (; a <= b; a += k) {sum_nod += nod(a);sum_sod += sod(a);}printf("%lld %lld\n", sum_nod, sum_sod);}return 0;
}
UVA12043 Divisors【欧拉筛法】相关推荐
- UVA11876 N + NOD (N)【欧拉筛法+前缀和】
Consider an integer sequence N where, N0 = 1 Ni = Ni−1 + NOD(Ni−1) for i > 0 Here, NOD(x) = n ...
- 质数c语言欧拉筛选,Python|欧拉筛法求质数
欢迎点击「算法与编程之美」↑关注我们! 本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章. 问题描述 我们知道第一个质数是 2.第二个质数是 3.第三个质 ...
- 素数计算之埃氏筛法、欧拉筛法
埃氏筛法 int main() {const int maxNumber=200;int isPrime[maxNumber];int i;int x;for (i=0;i<maxNumber; ...
- UVA516 POJ1365 LA5533 ZOJ1261 Prime Land【欧拉筛法】
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- UVA10738 Riemann vs Mertens【欧拉筛法】
One of the biggest, most mathematicians would call it THE biggest, unsolved problems in mathematics ...
- UVA12039 Goldbach‘s Cardinality【欧拉筛法】
Goldbach's cardinality (GC) of an even number means the number of ways the even number can be expres ...
- 欧拉筛法(线性筛)的学习理解
前言 在刚接触编程语言时,对于寻找素数,第一时间想到的便是二重循环暴力查找,其复杂度O(n^2),通过循环中只判断到根号n可以优化一些,不过复杂度也达不到预期.在数论的学习中,我学到了埃氏筛法,O(n ...
- 筛质数—(朴素筛法、埃氏筛法、欧拉筛法(线性筛法))
筛质数时首先要了解质数的定理:1~n中有 个质数 下面再来看具体算法: 1.朴素筛法: 直接把2~n-1中质数和合数的倍数都筛一遍,其代码如下所示: int primes[N],cnt=0; boo ...
- 素数筛法(传统普通、朴素筛法、埃式筛法、欧拉筛法(线性筛))
素数筛法(普通.朴素筛法.埃式筛法.欧拉筛法) 1.题目 2.分析 3.代码 传统普通 朴素筛法 朴素筛法(6.14) 埃式筛法 埃式筛法(6.14) 欧拉筛法(线性筛) 欧拉筛法(线性筛 6.14) ...
最新文章
- 我的linux Mint之路(三)
- esxi ntp服务器地址_NTP的工作原理以及工作模式
- tomcat日志切割
- ios APP开发简单实例
- ggplot2设置坐标轴范围_ggplot2画图时出现重合的点以及标签如何处理?有现成的包ggrepel
- html代码在线运行环境,ES5/可执行代码与执行环境
- c语言,递归翻转一个单链表,c实现单链表
- python为什么用class_python为什么会有@classmethod?
- timesten java_使用java调用timesten实现Oracle入库
- LintCode 38: Search a 2D Matrix II
- shark恒破解笔记2-绕过自校验
- Unity3d读取安卓手机相册
- 云服务优缺点_云服务器优点和缺点
- C++的O2、O3到底是个什么鬼
- JDK 1.7 基本概念和目录结构
- Wifi4更换Wifi6路由器的使用体验
- 深度学习电脑配置推荐
- 2.1微信小程序--比较数字大小
- 3D打印设备的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- 俯仰角,赤经赤纬,望远镜