2018秋招 京东笔试题 求幂

 求幂

东东对幂运算很感兴趣,在学习的过程中东东发现了一些有趣的性质: 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

#include<bits/stdc++.h>

#include <algorithm>
using namespace std;
const int mod =1e9+7;
set<int> S;
int n;
int main() {
    cin>>n;
    int res=1ll*n*(n*2-1)%mod;
    for(int i=2;i*i<=n;i++){
        if(S.find(i)!=S.end()) continue;//s中已存在
        long long tmp=1;
        int cnt=0;
        while(tmp<=n){
            S.insert(tmp);
            tmp=tmp*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;
            }
        } 
    }
	cout<<res;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/behboyhiex/article/details/82263146
今日推荐