J. Sum
A square-free integer is an integer which is indivisible by any square number except 111. For example, 6=2⋅36 = 2 \cdot 36=2⋅3 is square-free, but 12=22⋅312 = 2^2 \cdot 312=22⋅3 is not, because 222^222 is a square number. Some integers could be decomposed into product of two square-free integers, there may be more than one decomposition ways. For example, 6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab6 = 1\cdot 6=6 \cdot 1=2\cdot 3=3\cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ban=ba are considered different if a?=ba \not = ba?=b. f(n)f(n)f(n) is the number of decomposition ways that n=abn=abn=ab such that aaa and bbb are square-free integers. The problem is calculating ∑i=1nf(i)\sum_{i = 1}^nf(i)∑i=1n?f(i).
Input
The first line contains an integer T(T≤20)T(T\le 20)T(T≤20), denoting the number of test cases.
For each test case, there first line has a integer n(n≤2⋅107)n(n \le 2\cdot 10^7)n(n≤2⋅107).
Output
For each test case, print the answer ∑i=1nf(i)\sum_{i = 1}^n f(i)∑i=1n?f(i).
Hint
∑i=18f(i)=f(1)+?+f(8)\sum_{i = 1}^8 f(i)=f(1)+ \cdots +f(8)∑i=18?f(i)=f(1)+?+f(8)
=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.
样例输入
2 5 8
样例输出
8 14
题目来源
解析:题目求和1-n的各个数的非平方因子的和,与质数有关,或者用积性函数线性筛。
提交验证代码处:https://nanti.jisuanke.com/t/30999
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 5 int t; 6 LL n; 7 const int maxn = 22000000; 8 bool tag[maxn]; 9 int p[maxn/10]; 10 int cnt; 11 LL sum[maxn]; 12 void GetPrime() 13 { 14 cnt = 0; 15 sum[1] = 1; 16 for(int i = 2; i < maxn; i++) 17 { 18 if(!tag[i]) 19 { 20 p[cnt++] = i; 21 sum[i] = 2; 22 } 23 for(int j = 0; j < cnt && p[j] * i < maxn; j++) 24 { 25 tag[i * p[j]] = 1; 26 if(i % p[j] == 0) 27 { 28 int qq = 0, xx = i; 29 LL v = 1; 30 while (xx%p[j]==0) 31 { 32 qq ++; 33 v *= p[j]; 34 xx/=p[j]; 35 } 36 v *= p[j]; 37 if (qq >= 2) 38 { 39 sum[i*p[j]] = 0; 40 } 41 else if(qq == 1) 42 { 43 sum[i*p[j]] = sum[i] / 2; 44 } 45 break; 46 } 47 sum[i*p[j]] = sum[i] * 2; 48 } 49 } 50 for (int i = 1; i < maxn; i++) 51 { 52 sum[i] += sum[i-1]; 53 } 54 } 55 int main() 56 { 57 GetPrime(); 58 cin >> t; 59 while(t--) 60 { 61 scanf("%lld", &n); 62 printf("%lld\n", sum[n]); 63 } 64 return 0; 65 }
原文地址:https://www.cnblogs.com/weixq351/p/9573604.html