LightOj1007 - Mathematically Hard(欧拉函数)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1007

题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ≤ a ≤ b ≤ 5 * 106).

线性打表求值即可;

结果会爆long long,要用unsigned long long %llu形式;

#include <stdio.h>
#include <string.h>
#include <algorithm>
typedef unsigned long long LL;
#define N 5000001
using namespace std;

int f[N] = {0};
LL ans[N] = {0};

void Init()
{
    for(int i=2; i<N; i++)
    {
        if(f[i]) continue;
        for(int j=i; j<N; j+=i)
        {
            if(!f[j]) f[j] = j;
            f[j] = f[j]/i*(i-1);
        }
    }
    ans[0] = 0;
    for(int i=1; i<N; i++)
        ans[i] = ans[i-1] + (LL)f[i]*f[i];
}

int main()
{
    Init();

    int T, a, b, t = 1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &a, &b);
        printf("Case %d: %llu\n", t++, ans[b]-ans[a-1]);
    }
    return 0;
}

时间: 2024-08-10 13:15:15

LightOj1007 - Mathematically Hard(欧拉函数)的相关文章

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

1007 - Mathematically Hard PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 64 MBMathematically some problems look hard. But with the help of the computer, some problems can be easily solvable. In this problem, you will be given tw

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 .

LightOJ1007---Mathematically Hard (欧拉函数)

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 (inclusi

欧拉函数入门

欧拉函数:小于x的整数中与x互质的数的个数,一般用φ(x)表示,φ(1)=1 计算公式:φ(x)=x*(1-1/p1)(1-1/p2)...(1-1/pn),其中x的所有素因子数分别为p1,p2,p3,...,pn. 常用性质:1.对于素数p,φ(p) = p-1, φ(pk) = pk-pk-1 2.当gcd(n,m) = 1时,φ(n*m) = φ(n) * φ(m). 暴力求一个数n的φ(n)模板 1 /* */ 2 #include <iostream> 3 #include <

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=

POJ2478(SummerTrainingDay04-E 欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =

算法复习——欧拉函数(poj3090)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For exa