打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
欧拉函数

The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a) (a 1) .... (b)

Input

There are several test cases. Each line has two integers a, b (2<a<b<3000000).

Output

Output the result of (a) (a 1) .... (b)

Sample Input

3 100

Sample Output

3042

【题意】

给定区间a,b要求a到b的欧拉函数对应的值的和

直接上模板

#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>typedef long long ll;using namespace std;const int size=3000000;ll euler[size];void init(){    memset(euler,0,sizeof(euler));    euler[1]=1;    for(ll i=2;i<=size;i  )    {        if(euler[i]==0)        {            for(ll j=i;j<=size;j =i)            {                if(euler[j]==0)                {                    euler[j]=j;                }                euler[j]=euler[j]/i*(i-1);            }        }    }}int main(){    init();    ios::sync_with_stdio(false);    ll a,b;    while(cin>>a>>b)    {        ll sum=0;        for(ll i=a;i<=b;i  )            sum =euler[i];        cout<<sum<<endl;    }    return 0;}

后看到网上有人用前缀和做:

#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#define ll long longusing namespace std;const int size=3000000;ll euler[size 10];void init(){    memset(euler,0,sizeof(euler));    euler[1]=1;    for(ll i=2;i<=size;i  )    {        if(euler[i]==0)        {            for(ll j=i;j<=size;j =i)            {                if(euler[j]==0)                {                    euler[j]=j;                }                euler[j]=euler[j]/i*(i-1);                //先进行除法是为了防止中间数据的溢出            }        }    }    for(ll i=3;i<=size;i  )     //前缀和    {        euler[i] =euler[i-1];    }}int main(){    init();          //初始化,首先求出每个数对应的质因数个数    ll a,b;    while(~scanf("%lld%lld",&a,&b))    {        printf("%lld\n",euler[b]-euler[a-1]);    }    return 0;}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Euler函数
linux使用libcurl实现put访问服务器
数学考古——欧拉是如何思考的
[NOI Online 提高组]第三题-最小环 解析
扩展欧几里德算法详解
函数的形成与发展
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服