當前位置:
首頁 >
小b和灯泡(51Nod-2489)
發(fā)布時間:2025/3/17
27
豆豆
生活随笔
收集整理的這篇文章主要介紹了
小b和灯泡(51Nod-2489)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目
小b有n個關(guān)閉的燈泡,編號為1...n。
小b會進行n輪操作,第i輪她會將編號為i的倍數(shù)的燈泡的開關(guān)狀態(tài)取反,即開變成關(guān),關(guān)變成開。
求n輪操作后,有多少燈泡是亮著的。
輸入
輸入一個數(shù)字表示燈泡數(shù)n,其中1<n≤10000000
輸出
輸出一個數(shù)字表示最終亮著的燈泡數(shù)
輸入樣例
3
輸出樣例
1
思路:
實質(zhì)是求 1~n 中每個數(shù)因子的個數(shù),那么我們對 1~n 的每個數(shù)的因子數(shù)打一個表可以發(fā)現(xiàn):i 為奇數(shù)或 sqrt(i) 不為整數(shù)時,其因子個數(shù)為偶數(shù),而 sqrt(i) 為整數(shù)時,其因子個數(shù)為奇數(shù)
那么根據(jù)題意可知,當因子個數(shù)為奇數(shù)時,其最終狀態(tài)為亮,當因子個數(shù)為偶數(shù)時,其最終狀態(tài)為不亮
因此,該題本質(zhì)是求 1~n 中可開方數(shù)的個數(shù),即 sqrt(n)
源程序
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 100000+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std;int main(){LL n;cin>>n;cout<<(int)sqrt(n)<<endl;return 0; }?
總結(jié)
以上是生活随笔為你收集整理的小b和灯泡(51Nod-2489)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Array with Odd Sum(C
- 下一篇: 图论 —— 生成树 —— 增量最小生成树