ligtoj 1007 - Mathematically Hard(欧拉函数+前缀和)

1007 - Mathematically Hard
PDF (English) Statistics Forum
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."

思路: 用欧拉函数预处理出phi,  然后求下前缀和。

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

typedef unsigned long long ull;

ull phi[5000100];

void getPhi(){

    phi[1] = 1;
    for(int i=2;i<=5000010;i++){
        if(!phi[i]){

            for(int j=i;j<=5000010;j+=i){
                if(!phi[j]) phi[j] = j;
                phi[j] = phi[j]/i*(i-1);
            }

        }
    }
    for(int i=2;i<5000010;i++) phi[i] = phi[i]*phi[i]+phi[i-1];
    return ;
}

int main(){
    getPhi();
    int T,a,b;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%d%d",&a,&b);
        printf("Case %d: %llu\n",t,phi[b]-phi[a-1]);
    }

    return 0;
} 
时间: 2024-11-05 14:46:18

ligtoj 1007 - Mathematically Hard(欧拉函数+前缀和)的相关文章

light oj 1007 Mathematically Hard (欧拉函数)

题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数,若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N / a) * a:若(N % a == 0 && (N / a) % a != 0) 则有:E(N) = E(N / a) * (a - 1) 对于这题来说,首先卡MLE..只能开一个数组..所以把前缀和也存到欧拉数组里.然后卡long long..要用unsigned long long .

【欧拉函数】欧拉函数前缀和

转自http://www.cnblogs.com/chanme/p/4457200.html H.   Game Alice likes to play games. One day she meets such a game. There are N * N switches arranged in an N * N array. Pressing a switch would change its state, from off to onor from on to off. In addi

欧拉函数前缀和

今天软院校赛,有一道H题非常的神,所以记下来.题意转化了之后就是求欧拉函数的前缀和.自然的想法是O(n)的线性预处理可以求出前n个数的欧拉函数,又或者是O(sqrt(n))的预处理求出单个数的欧拉函数.但是题目要求的是前n(n<=10^9)个数欧拉函数的前缀和.于是我就觉得这是没法做的了,赛后问了出题人,出题人非常好的给了下面的这个链接. http://wzimpha.sinaapp.com/archives/596 然后大体的思路就在里面,这里也不重复了.主要是觉得这种方法好神,所以特别的在这

Farey Sequence POJ - 2478 (欧拉函数 前缀和)

Farey Sequence POJ - 2478 题目链接:https://vjudge.net/problem/POJ-2478 题目: 法理序列Fn是指对于任意整数n( n >= 2),由不可约的分数a/b(0 < a < b <= n),gcd(a,b) = 1升序排列构成的序列,最开始的几个如下 F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/5, 1/4, 1/3, 2/5,

【51nod-1239&amp;1244】欧拉函数之和&amp;莫比乌斯函数之和 杜教筛

题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 杜教筛裸题,不过现在我也只会筛这俩前缀和... $$s(n)=\sum _{i=1}^{n}f(i)$$ 那么就有: $$\sum_{i=1}^{n}f(i)\lfloor \frac{n}{i} \

HDU5780 gcd (BestCoder Round #85 E) 欧拉函数预处理——分块优化

分析(官方题解): 一点感想: 首先上面那个等式成立,然后就是求枚举gcd算贡献就好了,枚举gcd当时赛场上写了一发O(nlogn)的反演,写完过了样例,想交发现结束了 吐槽自己手速慢,但是发了题解后发现,这题连O(n)欧拉函数前缀和的都卡了,幸亏没交,还是太年轻 对于官方题解说sqrt(n)优化(其实就是n/(小于n一段数)结果是一样的,也不算什么分块),还是很简单的,做反演题的时候看到过很多,只是忘记了 如果不会请看这篇解题报告http://wenku.baidu.com/view/fbe2

欧拉函数-Product

原题地址 先吐槽一波:凉心出题人又卡时间又卡空间 先来化简一波柿子 \[\prod_{i=1}^{n}\prod_{j=1}^{n}\frac{lcm(i,j)}{gcd(i,j)}\] \[=\prod_{i=1}^{n}\prod_{j=1}^{n}\frac{i*j}{gcd(i,j)^2}\] \[=(\prod_{i=1}^{n}\prod_{j=1}^{n}i*j)*(\prod_{i=1}^{n}\prod_{j=1}^{n}gcd(i,j))^{-2}\] 先看前面的那一坨: \

莫比乌斯反演欧拉函数杜教筛大总结

莫比乌斯函数 定义 设\(n=\prod_{i=1}^{k} p_i^{c_i}\),则\(\mu(n)=(-1)^k\),特别地\(\mu(1)=1\). 性质 最常用性质 \(\sum_{d|n}\mu(d)=[n=1]\) 反演性质 \(F(n)=\sum_{d|n}f(d) \Longleftrightarrow f(n)=\sum_{d|n}F(d)\mu(\frac{n}{d})\) \(F(n)=\sum_{n|d}f(d) \Longleftrightarrow f(n)=\su

[NOI2010][bzoj2005] 能量采集 [欧拉函数+分块前缀和优化]

题面: 传送门 思路: 稍微转化一下,可以发现,每个植物到原点连线上植物的数量,等于gcd(x,y)-1,其中xy是植物的横纵坐标 那么我们实际上就是要求2*sigma(gcd(x,y))-n*m了 又有某不知名神奇定理:一个数的所有因子的phi之和等于这个数本身,其中phi是欧拉函数 因此题目转化为求如下: 我们把式子变个型,就成了如下式子: 然后一个前缀和优化,O(n+sqrt(n))解决 Code: 1 #include<iostream> 2 #include<cstdio>