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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[USACO16JAN]子共七Subsequences Summing to Sevens

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [USACO16JAN]子共七Subsequences Summing to Sevens 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

Farmer John's NNN cows are standing in a row, as they have a tendency to do from time to time. Each cow is labeled with a distinct integer ID number so FJ can tell them apart. FJ would like to take a photo of a contiguous group of cows but, due to a traumatic childhood incident involving the numbers 1…61 \ldots 616 , he only wants to take a picture of a group of cows if their IDs add up to a multiple of 7.

Please help FJ determine the size of the largest group he can photograph.

給你n個數,求一個最長的區間,使得區間和能被7整除

輸入輸出格式

輸入格式:

The first line of input contains NNN (1≤N≤50,0001 \leq N \leq 50,0001N50,000 ). The next NNN

lines each contain the NNN integer IDs of the cows (all are in the range

0…1,000,0000 \ldots 1,000,00001,000,000 ).

輸出格式:

Please output the number of cows in the largest consecutive group whose IDs sum

to a multiple of 7. If no such group exists, output 0.

輸入輸出樣例

輸入樣例#1: 復制 7 3 5 1 6 2 14 10 輸出樣例#1: 復制 5

說明

In this example, 5+1+6+2+14 = 28.

同余的性質

(a mod m) + (b mod m) ≡ a + b (mod m)
(a mod m) - (b mod m) ≡ a - b (mod m)
(a mod m) * (b mod m) ≡ a * b (mod m)

?

給出序列,求出最長的區間,滿足區間和為7的倍數
預處理出前綴和
(pre[j] - pre[i ]) mod 7 = 0
=> pre[i] ≡ pre[j] (mod 7)
維護7個值,分別表示模7余0到6的最前d的前綴和
掃一遍過去就行了

?

1 //2018年2月14日14:15:25 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int N = 100001; 8 int n, a[N], sum[N], last[N], first[N], ans; 9 10 int main(){ 11 scanf("%d", &n); 12 for(int i=1;i<=n;i++){ 13 scanf("%d", &a[i]); 14 sum[i] = (sum[i-1] + a[i]) % 7; 15 } 16 for(int i=1;i<=n;i++) 17 last[sum[i]] = i; 18 for(int i=n;i>=1;i--) 19 first[sum[i]] = i; 20 for(int i=0;i<7;i++) 21 ans = max(ans, last[i]-first[i]); 22 printf("%d\n", ans); 23 24 return 0; 25 }

?

轉載于:https://www.cnblogs.com/sineagle/p/8448354.html

總結

以上是生活随笔為你收集整理的[USACO16JAN]子共七Subsequences Summing to Sevens的全部內容,希望文章能夠幫你解決所遇到的問題。

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