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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

XXXXX CodeForces - 1364A(思维)

發布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XXXXX CodeForces - 1364A(思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Ehab loves number theory, but for some reason he hates the number x. Given an array a, find the length of its longest subarray such that the sum of its elements isn’t divisible by x, or determine that such subarray doesn’t exist.

An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Input
The first line contains an integer t (1≤t≤5) — the number of test cases you need to solve. The description of the test cases follows.

The first line of each test case contains 2 integers n and x (1≤n≤105, 1≤x≤104) — the number of elements in the array a and the number that Ehab hates.

The second line contains n space-separated integers a1, a2, …, an (0≤ai≤104) — the elements of the array a.

Output
For each testcase, print the length of the longest subarray whose sum isn’t divisible by x. If there’s no such subarray, print ?1.

Example
Input
3
3 3
1 2 3
3 4
1 2 3
2 2
0 6
Output
2
3
-1
Note
In the first test case, the subarray [2,3] has sum of elements 5, which isn’t divisible by 3.

In the second test case, the sum of elements of the whole array is 6, which isn’t divisible by 4.

In the third test case, all subarrays have an even sum, so the answer is ?1.

思路:一開始想復雜了,我們比較一下從左邊刪除數字和從右邊刪除數字,最終剩下的長度,選取最大的那個就行了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; int a[maxx]; int sum[maxx]; int n,x;int main() {int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&x);memset(sum,0,sizeof(sum));for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];if(sum[n]%x) cout<<n<<endl;else{int ans=-1;for(int i=1;i<=n;i++){if((sum[n]-sum[i])%x){ans=max(ans,n-i);break;}}for(int i=n;i>=1;i--){if(sum[i]%x){ans=max(ans,i);break;}}cout<<ans<<endl;}}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的XXXXX CodeForces - 1364A(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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