Lightoj 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
欧拉函数打表。抄的大白书上的板子。一开始没看note 自己yy筛法。果然很弱智,题目已经给了Note了,眼瞎。
/* ***********************************************
Author :guanjun
Created Time :2016/6/10 19:35:28
File Name :1007.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 5000100
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int phi[maxn];
ull sum[maxn];
void phi_table(int n){
for(int i=;i<=n;i++)phi[i]=;
phi[]=;
for(int i=;i<=n;i++)if(!phi[i]){
for(int j=i;j<=n;j+=i){
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
sum[]=;
for(int i=;i<=maxn;i++){
sum[i]=sum[i-]+(ull)phi[i]*phi[i];
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
phi_table(maxn);
int T,a,b;
cin>>T;
for(int t=;t<=T;t++){
scanf("%d%d",&a,&b);
printf("Case %d: %llu\n",t,sum[b]-sum[a-]);
}
return ;
}
Lightoj 1007 - Mathematically Hard的更多相关文章
- lightoj 1007 - Mathematically Hard 欧拉函数应用
题意:求[a,b]内所有与b互质个数的平方. 思路:简单的欧拉函数应用,由于T很大 先打表求前缀和 最后相减即可 初次接触欧拉函数 可以在素数筛选的写法上修改成欧拉函数.此外本题内存有限制 故直接计算 ...
- 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
预先处理好phi数组和前缀和,水题. #include<cstdio> #include<string> #include<cstring> #include< ...
- LightOj1007 - Mathematically Hard(欧拉函数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1007 题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ...
- SCNU 2015ACM新生赛初赛【1007. ZLM的扑克牌】解题报告
题目链接详见SCNU 2015新生网络赛 1007. ZLM的扑克牌 . 其实我在想这题的时候,还想过要不要设置求最小的排列,并且对于回文数字的话,可以把扑克牌折起来( ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- PKU 1007
题名:DNA排序 题意:给定字符串长度.个数,计算每个字符串的逆序数,然后从大到小排列,有兴趣的可以去看下原题. 计算字符串逆序数,然后排序,这里使用了快速排序算法,string释放的时候竟然有问题, ...
随机推荐
- Root of AVL Tree
04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...
- Linux常用命令大全--有关磁盘空间的命令
1.mount 命令的功能是挂载文件系统,可以挂载硬盘.光盘.软盘,也可以挂载NFS网络文件系统 mount -t 设备类型 存放目录 mount IP地址:/所提供的目录 存放目录 (无) 不加任何 ...
- nginx.conf的完整配置说明
#用户 用户组 user www www; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes 5: #错误日志 error_log logs/e ...
- 返回json格式的数据
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 2016 Multi-University Training Contest 6 solutions BY UESTC
A Boring Question \[\sum_{0\leq k_{1},k_{2},\cdots k_{m}\leq n}\prod_{1\leq j< m}\binom{k_{j+1}}{ ...
- [APIO2012] 派遣 dispatching
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4580 Solved: 2348 Description 在一个忍者的帮派里,一些忍者们被选中派遣给 ...
- 会修修的莫队--BZOJ2120: 数颜色
$n \leq 10000$的数列,$m \leq 10000$个操作,一:单点修改:二:查区间不同数字个数.修改数$\leq 1000$,数字$\leq 1000000$. 我不会告诉您这是三种写法 ...
- Visual Studio Code Edit
微软的跨平台编辑器~~ 下载地址(官网):https://code.visualstudio.com/ 下载地址(网盘):http://pan.baidu.com/s/1ntLy8Tr 使用技巧: c ...
- 一份关于webpack2和模块打包的新手指南(二)
插件 我们已经看到一个内置的webpack插件的例子,在npm run build脚本中调用的webpack -p命令就是使用webpack附带的UglifyJsPlugin插件以生产模式压缩打包文件 ...