ACM-ICPC 2018 南京赛区网络预赛 J.Sum
Posted zllwxm123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ACM-ICPC 2018 南京赛区网络预赛 J.Sum相关的知识,希望对你有一定的参考价值。
Sum
A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 cdot 36=2⋅3 is square-free, but 12 = 2^2 cdot 312=22⋅3 is not, because 2^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 = 1cdot 6=6 cdot 1=2cdot 3=3cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ba are considered different if a ot = ba?=b. f(n)f(n) is the number of decomposition ways that n=abn=ab such that aa and bb are square-free integers. The problem is calculating sum_{i = 1}^nf(i)∑i=1n?f(i).
Input
The first line contains an integer T(Tle 20)T(T≤20), denoting the number of test cases.
For each test case, there first line has a integer n(n le 2cdot 10^7)n(n≤2⋅107).
Output
For each test case, print the answer sum_{i = 1}^n f(i)∑i=1n?f(i).
Hint
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.
样例输入
2 5 8
样例输出
8 14
题目来源
题意:定义f[i]函数代表i=a*b的对数,其中a和b都不能是平方数的倍数,a*b与b*a不相同,t组样例,给出n,求1~n的f[i]之和
类似于素数筛+前缀和
1 #include <bits/stdc++.h> 2 #define ll long long int 3 #define N 20000002 4 using namespace std; 5 int sum[N]; 6 bool an[N]; 7 int bn[N]; 8 int cnt = 0; 9 void init(){ 10 an[0] = true; 11 for(int i=2;i*i<N;i++){ 12 ll k = i*i; 13 for(int j = k;j<N;j+=k){ 14 an[j] = true; 15 } 16 } 17 for(int i=1;i<N;i++){ 18 if(!an[i]){ 19 sum[i] = sum[i-1]+1; 20 bn[cnt++] = i; 21 }else 22 sum[i] = sum[i-1]; 23 } 24 } 25 int t,n; 26 int main(){ 27 ios::sync_with_stdio(false); 28 cin.tie(0),cout.tie(0); 29 init(); 30 cin>>t; 31 while(t--){ 32 cin>>n; 33 ll ans = 0; 34 for(int i=0;i<cnt&&bn[i]<=n;i++){ 35 int pox = n/bn[i]; 36 ans += sum[pox]; 37 } 38 cout<<ans<<endl; 39 } 40 return 0; 41 }
以上是关于ACM-ICPC 2018 南京赛区网络预赛 J.Sum的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2018 南京赛区网络预赛 Magical Girl Haze 最短路
ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang