洛谷P2158儀仗隊(數學,觀察找規律,歐拉函數)
阿新 • • 發佈:2018-10-14
show 找規律 == += 數值 std pro tchar urn
題目鏈接:https://www.luogu.org/problemnew/show/P2158
讀完題被嚇到了,這是什麽東西。
總之,需要觀察+找規律啊!觀察可以發現,從第三行開始,第i行中當前直角三角形範圍內能觀察到的點數等於(i-1)的歐拉函數值
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 using namespace std; 7 typedef longlong ll; 8 typedef unsigned long long ull; 9 const int maxn=1e3+5; 10 const int inf=1e9; 11 int vis[maxn]; 12 int n,ans; 13 14 int Euler(int n) 15 { 16 if(n==1) return 1; 17 int ans=n; 18 19 for(int i=2;i*i<=n;++i) 20 { 21 if(n%i==0) 22 { 23 while(n%i==0) n/=i; 24 ans=ans/i*(i-1); 25 } 26 } 27 if(n!=1) ans=ans/n*(n-1); 28 29 return ans; 30 } 31 32 int main() 33 { 34 ios::sync_with_stdio(false); cin.tie(0); 35 36 cin>>n; 37 38 if(n==1) 39 { 40 putchar(‘\0‘); 41 return 0; 42 } 43 for(int i=3;i<=n;i++) ans+=Euler(i-1); 44 45 cout<<ans*2+3<<endl; 46 47 //或者下面寫法 48 //for(int i=1;i<=n-1;i++) ans+=Euler(i); 49 //cout<<ans*2+1<<endl; 50 51 return 0; 52 }
完。
洛谷P2158儀仗隊(數學,觀察找規律,歐拉函數)