1007 - Mathematically Hard
Time Limit: 2 second(s) | Memory Limit: 64 MB |
Mathematically some problems look hard. But with the help of the computer, some problems can be easily solvable.
In this problem, you will be given two integers a and b. You have to find the summation of the scores of the numbers from a to b (inclusive). The score of a number is defined as the following function.
score (x) = n2, where n is the number of relatively prime numbers with x, which are smaller than x
For example,
For 6, the relatively prime numbers with 6 are 1 and 5. So, score (6) = 22 = 4.
For 8, the relatively prime numbers with 8 are 1, 3, 5 and 7. So, score (8) = 42 = 16.
Now you have to solve this task.
Input
Input starts with an integer T (≤ 105), denoting the number of test cases.
Each case will contain two integers a and b (2 ≤ a ≤ b ≤ 5 * 106).
Output
For each case, print the case number and the summation of all the scores from a to b.
Sample Input |
Output for Sample Input |
3 6 6 8 8 2 20 |
Case 1: 4 Case 2: 16 Case 3: 1237 |
Note
Euler's totient function applied to a positive integer n is defined to be the number of positive integers less than or equal to n that are relatively prime to n. is read "phi of n."
Given the general prime factorization of , one can compute using the formula
思路:就是求欧拉函数,先素数打表,再欧拉函数打表,最后求下前缀和;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 using namespace std;
8 bool pr[5*1000006]={0};
9 int prime[5*100006];
10 unsigned long long oula[5*1000006];
11 typedef long long LL;
12 int main(void)
13 {
14 LL i,j,p,q;
15 int s,k;
16 scanf("%d",&k);pr[0]=true;
17 pr[1]=true;
18 for(i=0;i<5*1000006;i++)
19 oula[i]=i;
20 for(i=2;i<10000;i++)
21 {
22 if(!pr[i])
23 {
24 for(j=i;i*j<=5*1000000;j++)
25 {
26 pr[i*j]=true;
27 }
28 }
29 }int cnt=0;
30 for(i=2;i<5*1000005;i++)
31 {
32 if(pr[i]==false)
33 prime[cnt++]=i;
34 }
35 for(i=0;i<cnt;i++)
36 {
37 for(j=1;prime[i]*j<=5*1000000;j++)
38 {
39 oula[prime[i]*j]=oula[prime[i]*j]/prime[i]*(prime[i]-1);
40 }
41 }
42 for(i=2;i<=1000000*5;i++)
43 {
44 oula[i]=oula[i-1]+oula[i]*oula[i];
45 }
46 for(s=1;s<=k;s++)
47 {
48 scanf("%lld %lld",&p,&q);printf("Case %d: ",s);
49 printf("%llu\n",oula[q]-oula[p-1]);
50 }
51 return 0;
52 }
1007 - Mathematically Hard的更多相关文章
- Lightoj 1007 - Mathematically Hard
1007 - Mathematically Hard PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 6 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- lightoj 1007 - Mathematically Hard 欧拉函数应用
题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...
- SCNU 2015ACM新生赛初赛【1007. ZLM的扑克牌】解题报告
题目链接详见SCNU 2015新生网络赛 1007. ZLM的扑克牌 . 其实我在想这题的时候,还想过要不要设置求最小的排列,并且对于回文数字的话,可以把扑克牌折起来( ...
- PKU 1007
题名:DNA排序 题意:给定字符串长度.个数,计算每个字符串的逆序数,然后从大到小排列,有兴趣的可以去看下原题. 计算字符串逆序数,然后排序,这里使用了快速排序算法,string释放的时候竟然有问题, ...
- sicily 1007. To and Fro 2016 11 02
// Problem#: 1007// Submission#: 4893204// The source code is licensed under Creative Commons Attrib ...
- 【HDU 1007】Quoit Design
http://acm.hdu.edu.cn/showproblem.php?pid=1007 最近欧式距离模板题. 用分治大法(分治的函数名用cdq纯属个人习惯_(:з」∠)_) 一开始狂M. 后来判 ...
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- poj 1007 DNA Sorting 解题报告
题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...
随机推荐
- [云原生]Docker - 简介
目录 什么是Docker? 为什么使用Docker? 对比传统虚拟机总结 什么是Docker? Docker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业务项目.它基于Go ...
- 25. Linux下gdb调试
1.什么是core文件?有问题的程序运行后,产生"段错误 (核心已转储)"时生成的具有堆栈信息和调试信息的文件. 编译时需要加 -g 选项使程序生成调试信息: gcc -g cor ...
- Set、Map、WeakSet 和 WeakMap 的区别
先总结: Set1. 成员不能重复2. 只有健值,没有健名,有点类似数组.3. 可以遍历,方法有add, delete,hasweakSet 1. 成员都是对象 2. 成员都是弱引用,随时可以消失. ...
- [Windows编程]模块遍历
模块遍历 整体思路 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 相关API的调用 创建进程快照API HANDLE WINAPI CreateToolhelp32Snap ...
- 循环队列/顺序队列(C++)
队列(queue)是一种限定存取位置的线性变.他允许在表的一端插入,在另一端删除.这个和计算机调度策略中的先来先服务FCFS(First Come/First Served)是一样的.队列中可以插入的 ...
- 银联acp手机支付总结
总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...
- redis 之 集群
#:下载源码包,并编译安装 [root@localhost src]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz [root ...
- my42_Mysql基于ROW格式的主从同步
模拟主从update事务,从库跳过部分update事务后,再次开始同步的现象 主库 mysql> select * from dbamngdb.isNodeOK; +----+--------- ...
- 【Spring Framework】Spring入门教程(二)基于xml配置对象容器
基于xml配置对象容器--xml 标签说明 alias标签 作用:为已配置的bean设置别名 --applicationContext.xml配置文件 <?xml version="1 ...
- Spring MVC与html页面的交互(以传递json数据为例)
一.导入相jar包 主要包括spring相关jar包和fastjson jar包,具体步骤略. 二.配置相关文件 1.配置web.xml文件 <?xml version="1.0&qu ...