线性素数筛 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>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<map>
#include<bitset>
#include<stack>
#include<vector>
#include<set>
using namespace std;
#define MAXN 1010
#define MAXM 1010
#define INF 1000000000
#define MOD 1000000007
#define ll long long
#define eps 5e-6
ll n, N;
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
const int maxn=2e7+1e5;

int num = 0;
int vis[maxn], pri[maxn];
ll f[maxn];
ll s[maxn];
void init() {
    rep(i, 1, maxn)f[i] = 1;
    mmm(vis, 0);
    mmm(pri, 0);
    num = 0;
}
void getpri() {
    for ( ll i = 2; i<N; i++) {
        if (!vis[i])pri[++num] = i,f[i]=2;
        for ( ll j = 1; j <= num && pri[j] * i<N; j++) {
            vis[pri[j] * i] = 1;
            f[pri[j] * i] *= f[pri[j]]*f[i];
            if (i%pri[j] == 0) { //二次方
                f[pri[j] * i] /= 4;
                if (i%(pri[j] * pri[j]) == 0)f[pri[j] * i] = 0;//三次方以上
                break;
            }//如果i遇到了自己最小的质因子就直接跳出
        }
    }
}
int main() {
    int t;
    cin >> t;
    int x = maxn;
    N = maxn;
    init();
    getpri();
    s[1] = 1;
    rep(i, 2, x) {
        s[i] =s[i-1]+ f[i];
    }
    while (t--)
    {
        cin >> n;
        cout << s[n]<< endl;
    }
    //cin >> t;
    return 0;
}
/*
1
8
20000000
2
5
8

*/

原文地址:https://www.cnblogs.com/SuuT/p/9571597.html

时间: 2024-10-10 19:39:18

线性素数筛 ACM-ICPC 2018 南京赛区网络预赛 J Sum的相关文章

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

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 南京赛区网络预赛 做题记录

比赛的时候被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

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 南京赛区网络预赛 K. The Great Nim Game

题意:有N堆石子(N为大数),每堆的个数按一定方式生成,问先手取若干堆进行尼姆博弈,必胜的方式有多少种. 题解:因为 k < 4096,所以出现的数最多只有4096个,对每个数字只考虑出现奇/偶次进行dp,答案是所有不等于0的dp值的和.然后如果一个数字出现x次,它对自己出现奇数次的方案数和偶数次的方案数贡献都是乘上2 ^ (x - 1),每个数字的贡献都是2 ^ (x - 1) 总贡献就是2 ^ (N - ∑(vis[i])).大数可以一边读入一边取模. 1 #include <bits/s