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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ 2356 Find a multiple 神题 传说中的经典.

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2356 Find a multiple 神题 传说中的经典. 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

Description

The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input

The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output

In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.

If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input

5
1
2
3
4
1
Sample Output

2
2
3
也就是說,在n個數里取一些數使它們的和是n的倍數,輸出取的數的個數以及每個數.如果無法取出則輸出0.

題解

我們寫過指數暴力,打過搜索,因為數據范圍加老爺機跪掉了.我不得不看了看題解.
確實精彩.我們對數列求前綴和,如果前i個的和對n取模為0,則直接輸出1-i的數,否則因為前綴和取模有n個值,但只有n-1種可能(沒有0),所以必然存在2個前綴和是相同的,直接輸出這兩個位置之間的所有數即可.小學學的抽屜原理,到這里就忘了.

#include<cstdio> using namespace std; const int boss=1e4; int a[boss+10],s[boss+10],vis[boss+10],n;//vis表示對n取模的值. int main() { int i; scanf("%d",&n); for (i=1;i<=n;i++) scanf("%d",&a[i]),s[i]=s[i-1]+a[i]; vis[0]=1;//把0標記一下 for (i=1;i<=n;i++) {if (vis[s[i]%n]){int k=vis[s[i]%n];printf("%d\n",i-k+1);for (int j=k;j<=i;j++) printf("%d\n",a[j]);return 0;}vis[s[i]%n]=i+1;} }//哈哈哈哈

總結

以上是生活随笔為你收集整理的POJ 2356 Find a multiple 神题 传说中的经典.的全部內容,希望文章能夠幫你解決所遇到的問題。

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