# [C語言] b836. kevin戀愛攻略系列題-2 說好的霸王花呢?? ## C語言 ### 觀念: 1. 找出規律 2. 請熟記等差數列公式! ![image](https://hackmd.io/_uploads/S1vVUh2Ep.png)[3] 距離第一次撰寫後的一個月後,重新思考: 一開始:剩下n 第一次拔:剩下n-1-m 第二次拔:剩下n-1-m-m ... 最後一次拔目標是剩下0, 若剩下0, 則印出Go Kevin!! 否則,印出No Stop!! 所以我們發現只要最後一項是0就好,所以我們不考慮**級數的合**,只考慮**最後一項**。 根據等差數列公式: 最後一項=第一項+(項數-1)乘以公差 所以要判斷 n+(項數-1)*m是否為0 ### 程式碼1: TLE(此程式碼會超時, 僅供參考) ``` #include <stdlib.h> #include <stdio.h> int main() { long long int m = 0, n = 0, i = 0; while(scanf("%lld %lld", &n, &m)!=EOF){ // 檢查 m 是否為0 if (m == 0) { printf("Go Kevin!!\n"); } // 直接計算滿足條件的 i 的值 else { for(i=1;n-1+m*(i-1)>=0;i++){ if(n-1+m*(i-1)==0){ printf("Go Kevin!!\n"); } else{ printf("No Stop!!\n"); } } } } return 0; } ``` 根據 [[2]](https://chat.openai.com)建議: 直接計算滿足條件i的值。 $n-1+m*(i-1)=0$ $m*(i-1)=-n+1$ $(i-1)=(-n+1)/m$ $i=((-n+1)/m)+1$ $i=((1-n)/m)+1$ $i=((1-n)/m)+m/m$ $i=(1-n+m)/m$ ### 程式碼2 AC: ``` #include <stdlib.h> #include <stdio.h> int main() { long long int m = 0, n = 0, i = 0; while(scanf("%lld %lld", &n, &m)!=EOF){ // 檢查 m 是否為0 if (m == 0) { printf("Go Kevin!!\n"); } // 直接計算滿足條件的 i 的值 else { if( (1-n+m) % m == 0){ printf("Go Kevin!!\n"); } else{ printf("No Stop!!\n"); } } } return 0; } ``` ## Reference: [1] 題目來源:[高中生程式解題系統](https://zerojudge.tw/ShowProblem?problemid=b836) [2] [ChatGPT](https://chat.openai.com) [3] [等差級數前n項和公式的重點整理與介紹](https://www.liveism.com/live-concept.php?q=%E7%AD%89%E5%B7%AE%E7%B4%9A%E6%95%B8%E5%89%8Dn%E9%A0%85%E5%92%8C%E5%85%AC%E5%BC%8F%E7%9A%84%E9%87%8D%E9%BB%9E%E6%95%B4%E7%90%86%E8%88%87%E4%BB%8B%E7%B4%B9)