日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively.?
The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on.?

Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party.?

Input

The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case.?

Output

For each test case specified by n output on a single line the n-th lucky number.?

Sample Input

1 2 10 20 0

Sample Output

2 3 29 83

解題報告:

? ?這題剛開始就想錯了,簡單的想成了素數問題,所以上來就是個線性篩,結果wa了。。。后來一想發現并不是素數問題,因為在這種模式下有的素數是要被篩掉的。主要是因為這個題意:是數到第k個還健在的數字,并且篩掉這個數字,這就導致了,篩掉的數字不全是素數,也有一部分是合數,因為這里找到的i并不是數學上的約數的意義,換句話來講,此時的i不是+i了,而有可能是+(i+1),或是+(i+3),+(i+2)等等、、、

? ?ps :? 這個打表的時間復雜度好像不是很好解釋?暫時弄不明白、、、

AC代碼:

//打表、、 #include<bits/stdc++.h>using namespace std;bool prime[40004]; int lucky[3005]; int n; int main() {memset(prime,1,sizeof prime);for(int i = 1; i<=3000; i++) {for(int j = 2; j<=40000; j++) {if(prime[j]) {lucky[i] = j;prime[j]=0;int sum = 0;for(int k = j+1; k<=40000; k++) {if(prime[k]) {sum++;if(sum % j == 0) prime[k] = 0;}}break;}}}while(scanf("%d",&n)) {if(n == 0 ) break;printf("%d\n",lucky[n]);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。