京东2018秋招笔试题之求幂

版权声明:博主GitHub网址https://github.com/29DCH,欢迎大家前来交流探讨和fork。 https://blog.csdn.net/CowBoySoBusy/article/details/81878196

东东对幂运算很感兴趣,在学习的过程中东东发现了一些有趣的性质: 9^3 = 27^2, 2^10 = 32^2
东东对这个性质充满了好奇,东东现在给出一个整数n,希望你能帮助他求出满足 a^b = c^d(1 ≤ a,b,c,d ≤ n)的式子有多少个。
例如当n = 2: 1^1=1^1
1^1=1^2
1^2=1^1
1^2=1^2
2^1=2^1
2^2=2^2
一共有6个满足要求的式子
输入描述:
输入包括一个整数n(1 ≤ n ≤ 10^6)

输出描述:
输出一个整数,表示满足要求的式子个数。因为答案可能很大,输出对1000000007求模的结果
示例1
输入
2
输出
6

知识点:set集合无重复数据,C++ STL函数。(i ^ x) ^ c = (i ^ y) ^ d 其实意味着x * c = y * d, 意味着(x / y) = (d / c),x,y可以枚举得到,通过预处理,其实就是求有多少对(c,d)。

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
set<int> S;
int n;
int main()
{
    scanf("%d",&n);
    int res=1LL*n*(n*2-1)%mod;
    for(int i=2; i*i<=n; i++)
    {
        if(S.find(i)!=S.end())
            continue;
        long long temp=i;
        int cnt=0;
        while(temp<=n)
        {
            S.insert(temp);
            temp=temp*i;
            cnt++;
        }
        for(int i=1; i<=cnt; i++)
        {
            for(int j=i+1; j<=cnt; j++)
            {
                res=(res+n/(j/__gcd(i,j))*2LL)%mod;
            }
        }
    }
    printf("%d\n",res);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CowBoySoBusy/article/details/81878196
今日推荐