因子个数筛

https://www.51nod.com/Challenge/Problem.html#problemId=2489

题意:

小b有n个关闭的灯泡,编号为1...n。

小b会进行n轮操作,第i轮她会将编号为i的倍数的灯泡的开关状态取反,即开变成关,关变成开。

求n轮操作后,有多少灯泡是亮着的。

解法:线性筛出因子个数,判断因子个数奇偶性。

#include<bits/stdc++.h>
typedef long long ll ;
#define int ll
#define mod 1000000007
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//ll lcm(ll a , ll b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define cin(x) scanf("%lld" , &x);
using namespace std;
const int maxn = 1e6+9;
const int N = 1e7+9;
int fact[N];


void factorseive(){
    for(int i = 1 ; i * i <= N ; i++){
        for(int j = i ; i * j <= N ; j++){
            fact[i*j] += 2 ;
            if(i == j) fact[i*j] -= 1 ;
        }
    }
}

void solve(){
    factorseive();
    int n ;
    scanf("%lld" , &n);
    int ans = 0 ;
    for(int i = 1 ; i <= n ; i++){
        if(fact[i]%2 == 1){
            ans++;
        }
    }
    cout << ans << endl;

}

signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0); cout.tie(0);
    //int t ;
    //cin(t);
    //while(t--){
        solve();
    //}
}

 

原文链接: https://www.cnblogs.com/nonames/p/12432118.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    因子个数筛

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/371678

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月3日 上午10:54
下一篇 2023年3月3日 上午10:55

相关推荐