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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

題目

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個(gè)數(shù)里取一些數(shù)使它們的和是n的倍數(shù),輸出取的數(shù)的個(gè)數(shù)以及每個(gè)數(shù).如果無法取出則輸出0.

題解

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

#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標(biāo)記一下 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;} }//哈哈哈哈

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。