CF1372B - Omkar and Last Class of Math(貪心+數學規律+數論+普及級)
阿新 • • 發佈:2021-10-14
2021.10.14補題
);對於奇數 \(n\),答案恆為 \(n\) 最小的因數 \(x\) 與 \(n-x\) ,即
CF1372B - Omkar and Last Class of Math(源地址自⇔CF1372B)
Problem
Example
3
4
6
9
2 2
3 3
3 6
tag:
⇔貪心、⇔數學規律、⇔數論、⇔*1300
題意:
將題目給定的 \(c\) 拆分成 \(a+b=c\),求出使得 \(LCM(a,b)\) 最大的 \(a\) 與 \(b\)。
思路:
暴力打表尋找規律,發現對於偶數 \(n\),答案恆為 cout<<n/2<<" "<<n/2<<endl;
(最大的 \(LCM(a,b)\) 即為 \(\frac{n}{2}\)
cout<<divi(n)<<" "<<n-divi(n)<<endl;
。
證明不詳。
AC程式碼:
//A WIDA Project #include<bits/stdc++.h> using namespace std; #define LL long long //=============================================================== LL T,ans,n; //=============================================================== LL divi(LL n){ for(LL i=2;i*i<=n;i++){ if(n%i==0){ ans=i; return ans; } } return 0; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) cin>>T; while(T-->0){ ans=0; cin>>n; if(n%2==0) cout<<n/2<<" "<<n/2<<endl; else{ ans=divi(n); if(ans==0) cout<<1<<" "<<n-1<<endl; else cout<<n/ans<<" "<<n-n/ans<<endl; } } return 0; }
錯誤次數:2次
原因:分解因數沒有優化到 \(O(\sqrt{n})\) ,導致超時。
原因:未考慮給定的數字是質數的情況(最小因數為1,這種情況程式碼中ans=0),導致除以0的情況發生,RE一次。
文 / WIDA
2021.10.14成文
首發於WIDA個人部落格,僅供學習討論
更新日記:
2021.10.14 成文