HDU 3161 Iterated Difference 暴力

Iterated Difference

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 786    Accepted Submission(s): 505

Problem Description

You
are given a list of N non-negative integers a(1), a(2), ... , a(N). You
replace the given list by a new list: the k-th entry of the new list is
the absolute value of a(k) - a(k+1), wrapping around at the end of the
list (the k-th entry of the new list is the absolute value of a(N) -
a(1)). How many iterations of this replacement are needed to arrive at a
list in which every entry is the same integer?

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

The
input will contain data for a number of test cases. For each case,
there will be two lines of input. The first line will contain the
integer N (2 <= N <= 20), the number of entries in the list. The
second line will contain the list of integers, separated by one blank
space. End of input will be indicated by N = 0.

Output

For
each case, there will be one line of output, specifying the case number
and the number of iterations, in the format shown in the sample output.
If the list does not attain the desired form after 1000 iterations,
print ‘not attained‘.

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

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100
const int inf=0x7fffffff;   //无限大
ll n[maxn];
ll n2[maxn];
int N;
int main()
{
    int cas=1;
    int N;
    while(cin>>N)
    {
        if(N==0)
            break;
        for(int i=0;i<N;i++)
        {
            cin>>n[i];
            n2[i]=n[i];
        }
        ll ans=0;
        int flag=0;
        int kiss=0;
        for(int i=0;i<N;i++)
        {
            if(n[i]!=n[(i+1)%N])
            {
                kiss=0;
                break;
            }
            if(i==N-1)
                kiss=1;
        }
        while(kiss==0&&ans<=1000)
        {

            for(int i=0;i<N;i++)
            {
                n[i]=fabs(n2[i]-n2[(i+1)%N]);
            }
            for(int i=0;i<N;i++)
            {
                n2[i]=n[i];
            }
            ans++;
            for(int i=0;i<N;i++)
            {
                if(n[i]!=n[(i+1)%N])
                {
                    kiss=0;
                    break;
                }
                if(i==N-1)
                    kiss=1;
            }
        }
        if(kiss==1)
            printf("Case %d: %d iterations\n",cas,ans);
        else
            printf("Case %d: not attained\n",cas);
        cas++;
    }
    return 0;
}
时间: 2024-08-10 15:11:37

HDU 3161 Iterated Difference 暴力的相关文章

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

HDU - 2089 不要62 (暴力或数位DP)

Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列. 你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

hdu 4775 Infinite Go(暴力)

题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋,总共走了n步,黑棋和白棋交替走,如果一片棋的上下左右被封死,那么该片棋子就会被吃掉,问说最后黑白棋各剩多少个. 解题思路:比较恶心的模拟题,相邻相同色的棋子要用并查集连接,并且要记录每片棋子还剩的空格数,如果空格数为0的话说明该片棋子被其他颜色围住,则要剔除掉,不且将相邻的位置不同色的棋空格数加1.主要是细节上的问题. 样例 8 7 5 5 4 5 3 5 3 4 4 4 3 3 4 6 18 1 3 1 4 2 2 1

hdu 2615 Division(暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2615 题解:挺简单的暴力枚举,小小的分治主要是看没人写题解就稍微写一下 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; ll a[123] , ans , sum[123

hdu 1496 Equations (暴力+hash)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=1496 对于方程a*x1^2+b*x2^2+c*x3^2+d*x4^2=0,给出a,b,c,d,求出有多少种方法使得方程成立,xi!=0,属于[-100,100] a,b,c,d也不为0,属于[-50,50]. Sample Input 1 2 3 -4 1 1 1 1 Sample Output 39088 0 题目分析: 直接暴力的话,会100^4,,超时,我们可以把等式转化为a*x1^2+b*