zoukankan      html  css  js  c++  java
  • SPOJ:NT Games(欧拉函数)

    Katniss Everdeen after participating in Hunger Games now wants to participate in NT Games (Number Theory Games).

    As she begins President Snow provides her a number k. Then, she has to defend t back to back attacks from Haymitch Abernathy for practice. In each attack Haymitch Abernathy gives two numbers l and r, for defense she has to compute :

    IMAGE

    As she is new to number theory, help her by computing given expression.

    Input Format

    First line contain an integer, i.e. k.

    Second line contain an integer, i.e. t.

    Each of next t lines contain two integers, i.e. l & r.

    Constraints

    1<=k<=10^5

    1<=t<=10^5

    1<=l<=10^5

    l<=r<=10^5

    Output Format

    For each attack output the value of expression.

    Sample Input

    1

    1

    1 5

    Sample Output

    26

    Explanation : Just evaluate the expression.

    题意: 求题意的区间的GCD^K之和模Mod

    思路:利用前缀和思想+欧拉函数:

                Σx  (GCD(i,j)==x,j>i),枚举X,然后枚举j,根据欧拉函数得到i的数量。    

               由于询问次数多,我们预处理出答案,预处理的时候,利用前缀和思想降低复杂度。

               总的复杂度=N*(N/1+N/2+N/3+N/4+...N/N)=NlogN。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    const int maxn=100000;
    const int Mod=1e9+7;
    ll phi[maxn+10],p[maxn+10],vis[maxn+10];
    ll ans[maxn+10],K,T,L,R,cnt;
    ll qpow(ll a,ll x){ ll  res=1; while(x){ if(x&1) res=res*a%Mod; a=a*a%Mod; x>>=1;} return res;}
    void getphi()
    {
        for(ll i=2;i<=maxn;i++){
            if(!vis[i]) p[++cnt]=i,phi[i]=i-1;
            for(ll j=1;j<=cnt&&p[j]*i<=maxn;j++){
                vis[i*p[j]]=1;
                phi[i*p[j]]=phi[i]*(p[j]-1);
                if(i%p[j]==0){
                    phi[i*p[j]]=phi[i]*p[j];
                    break;
                }
            }
        }
    }
    void solve()
    {
        for(ll i=1;i<=maxn;i++) ans[i]=(ans[i]+qpow(i,K))%Mod;//自己 
        for(ll i=1;i<=maxn;i++) ans[i]=(ans[i]+phi[i])%Mod;//1
        for(ll i=2;i<=maxn;i++){
            for(ll j=2;j*i<=maxn;j++)
              ans[i*j]=(ans[i*j]+qpow(i,K)*phi[j]%Mod)%Mod;
        }
        for(ll i=1;i<=maxn;i++) ans[i]=(ans[i-1]+ans[i])%Mod;
    }
    int main()
    {
        getphi();
        scanf("%lld%lld",&K,&T);    
        solve();
        while(T--){
            scanf("%lld%lld",&L,&R);
            printf("%lld
    ",((ans[R]-ans[L-1])%Mod+Mod)%Mod);
        }
        return 0;
    }
  • 相关阅读:
    iconv 文件编码相互转换
    MySQL 字符编码
    MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法
    PHP 输出日志到文件 DEMO
    Nginx http -> https 跳转后 POST 丢失
    SSH SCP 远程密钥登录配置和服务器间的文件传输
    Mac 安装 7zip
    git pull There is no tracking information for the current branch.
    MacOS 安装配置 Laravel
    Mac OS 安装 MySQL5.7
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8891770.html
Copyright © 2011-2022 走看看