Lost Array(CF-1043B)
Problem Description
Bajtek, known for his unusual gifts, recently got an integer array x0,x1,…,xk?1.
Unfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hours spent on searching for a new toy, Bajtek found on the arrays producer's website another array aa of length n+1. As a formal description of a?says, a0=0 and for all other i (1≤i≤n) ai=x(i?1)mod k+ai?1, where pmodqpmodq denotes the remainder of division p by q.
For example, if the x=[1,2,3]?and n=5, then:
a0=0,
a1=x0mod3+a0=x0+0=1,
a2=x1mod3+a1=x1+1=3,
a3=x2mod3+a2=x2+3=6,
a4=x3mod3+a3=x0+6=7,
a5=x4mod3+a4=x1+7=9.
So, if the x=[1,2,3] and n=5, then a=[0,1,3,6,7,9].
Now the boy hopes that he will be able to restore x?from a! Knowing that 1≤k≤n, help him and find all possible values of k — possible lengths of the lost array.
Input
The first line contains exactly one integer n (1≤n≤1000) — the length of the array a, excluding the element a0.
The second line contains nn integers a1,a2,…,an (1≤ai≤106).
Note that a0 is always 0 and is not given in the input.
Output
The first line of the output should contain one integer ll denoting the number of correct lengths of the lost array.
The second line of the output should contain ll integers — possible lengths of the lost array in increasing order.
Examples
Input
5
1 2 3 4 5
Output
5
1 2 3 4 5
Input
5
1 3 5 6 8
Output
2
3 5?
Input
3
1 5 3
Output
1
2
題意:給出 n 個數以及數組 a,要求出 x 數組的長度,并輸出 x 數組
思路:先根據 a 數組將所有 x 數組求出來,然后枚舉長度,依次判斷是否合法即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-6 #define MOD 16007 #define INF 0x3f3f3f3f #define N 1001 #define LL long long using namespace std; int a[N]; int sub[N]; int x[N]; int main() {int n;cin>>n;for(int i=1;i<=n;i++){cin>>a[i];sub[i]=a[i]-a[i-1];}int k=0;for(int i=1;i<=n;i++){bool flag=true;for(int j=i+1;j<=n;j++){if(sub[j]!=sub[j-i]){flag=false;break;}}if(flag){k++;x[k]=i;}}cout<<k<<endl;for(int i=1;i<=k;i++)cout<<x[i]<<" ";cout<<endl;return 0; }?
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Lost Array(CF-1043B)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图论算法 —— 图论概述
- 下一篇: 暑期训练日志----2018.8.3