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 = 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=ba are considered different if a \not = 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(T\le 20)T(T≤20), denoting the number of test cases.

For each test case, there first line has a integer n(n \le 2\cdot 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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题意:定义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 }

 

原文地址:https://www.cnblogs.com/zllwxm123/p/9573185.html

时间: 2024-10-10 13:21:59

ACM-ICPC 2018 南京赛区网络预赛 J.Sum的相关文章

线性素数筛 ACM-ICPC 2018 南京赛区网络预赛 J Sum

https://www.jisuanke.com/contest/1555?view=challenges 题意: 题解:写完都没发现是个积性函数233 想法就是对x分解质因数,f(x)就是2^k,其中k是x分解结果中次数为一的质因子个数.如果有某个次数大于等于3,f(x)==0; 这样明显会TLE 所以就想一个递推的方法. 于是魔改了一下线性筛. #include<iostream> #include<cstdlib> #include<cstdio> #includ

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the h

ACM-ICPC 2018 南京赛区网络预赛

A An Olympian Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; ll T,n; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld",&n); printf("%lld\n",n-1); } } B The writing on the w

ACM-ICPC 2018 南京赛区网络预赛 - C GDY (模拟)

诶,比赛时差一点就debug出来了 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int mod = 14; map<int,int> vz[205]; LL ans[205]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.tx

ACM-ICPC 2018 南京赛区网络预赛 做题记录

比赛的时候被J题卡常然后B题自闭最后G题没调完 5题gg 现在补题进度:6/12 A. 题解: 高考数学题,裂项完之后答案就是n!-1 (n!)%n=0,所以就是n-1 1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int T; 5 ll n; 6 int main() 7 { 8 cin>>T; 9 while(T--) 10 { 11 cin>>n; 12 cout