Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
For example, let N = 4 and start with the list (0 2 5 11). The successive iterations are:
2 3 6 11
1 3 5 9
2 2 4 8
0 2 4 6
2 2 2 6
0 0 4 4
0 4 0 4
4 4 4 4
Thus, 8 iterations are needed in this example.
Input
Output
Sample Input
4 0 2 5 11 5 0 2 5 11 3 4 300 8600 9000 4000 16 12 20 3 7 8 10 44 50 12 200 300 7 8 10 44 50 3 1 1 1 4 0 4 0 4 0
Sample Output
Case 1: 8 iterations Case 2: not attained Case 3: 3 iterations Case 4: 50 iterations Case 5: 0 iterations Case 6: 1 iterations
Source
The 2011 Rocky Mountain Regional Contest
#include<bits/stdc++.h>
using namespace std;
int a[30];
int main()
{
int n,ca=1;
while(scanf("%d",&n)&&n){
int k=1;
for(int i=0;i<n;i++) {scanf("%d",&a[i]);if(i!=0&&a[i]!=a[i-1]) k=0;}
if(n==1) k=1;
//if(k!=1 && k%2==1) k=2;
a[n]=a[0];
//for(int j=0;j<=n;j++) printf("%d ",a[j]);
// printf("\n");
int cnt=0;
while(k==0){
k=1;
// for(int j=0;j<=n;j++) printf("%d ",a[j]);
// printf("\n");
int i;
for(i=0;i<n;i++){
a[i]=abs(a[i+1]-a[i]);
if(i!=0&&a[i]!=a[i-1]) k=0;
}
a[n]=a[0];
cnt++;
if(cnt>1000) { k=2; break; }
}
if(k==1)
printf("Case %d: %d iterations\n",ca++,cnt);
else if(k==2)
printf("Case %d: not attained\n",ca++);
}
return 0;
}