uva 1594 Ducci Sequence 哈希

用了一个滚动数组

转化为字符串哈希问题

复杂度不是很大所以用set直接搞

如果数据规模大一点的话考虑用vector实现链地址法

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int maxn = 20;
const ull b = 9973;

int main()
{
   // freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    int a[2][maxn];

    while(T--)
    {
        set<ull> check;
        int cur = 0;

        int n;
        scanf("%d", &n);

        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[cur][i]);
        }

        ull e = 0;
        for(int i = 0; i < n; i++)
        {
            e = e*b + a[cur][i];
        }
        check.insert(e);

        bool ans = true;

        for(int k = 0; k < 1010; k++)
        {
            for(int i = 0; i < n; i++)
            {
                a[cur^1][i] = abs(a[cur][i] - a[cur][(i+1)%n]);
            }

            cur ^= 1;

            e = 0;
            for(int i = 0; i < n; i++)
            {
                e = e*b + a[cur][i];
            }

            if(e == 0)
            {
                ans = false;
                break;
            }
            else if(check.count(e))
            {
                ans = true;
                break;
            }
        }

        if(ans)
            printf("LOOP\n");
        else
            printf("ZERO\n");

    }

    return 0;
}
时间: 2024-11-08 04:39:58

uva 1594 Ducci Sequence 哈希的相关文章

紫书第五章训练 uva 1594 Ducci Sequence by crq

Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a

UVa 1594 - Ducci Sequence

没看清题号,TimeLimit.啊.  直接计算次数即可. 代码 : import java.util.*; public class Main1954 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- > 0) { int[] a = new int[16]; int n = scan.nextInt(); for

UVA 1594 - Ducci Sequence(暴力模拟)

想麻烦了.这题真的那么水啊..直接暴力模拟,1000次(看了网上的200次就能A)后判断是否全为0,否则就是LOOP: 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include

Ducci Sequence解题报告

A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2 - a3

LIS UVA 10534 Wavio Sequence

题目传送门 1 /* 2 LIS:应用,nlogn的做法,首先从前扫到尾,记录每个位置的最长上升子序列,从后扫到头同理. 3 因为是对称的,所以取较小值*2-1再取最大值 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-5 21:38:32 8 * File Name :UVA_10534.cpp 9 ***************

uva 1390 - Interconnect(期望+哈希+记忆化)

题目连接:uva 1390 - Interconnect 题目大意:给出n表示有n个点,m表示有m条边,现在任选两点建立一条边,直到整个图联通,问说还需建立边数的期望,建过边的两点仍可以建边. 解题思路:哈希的方法很是巧妙,将各个联通分量中节点的个数c[i]转换成一个30进制的数(因为节点个数最多为30),因为结果很大,所以对1e5+7取模.获得的哈希值作为插入和搜索的起点. #include <cstdio> #include <cstring> #include <alg

UVa 1584 Circular Sequence --- 水题

UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较二者字典序大小的函数, 然后再用一层循环,进行n次比较,保存最小的字典序的串的首字母位置,再利用模运算输出即可 /* UVa 1584 Circular Sequence --- 水题 */ #include <cstdio> #include <cstring> //字符串s为环状,

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

Ducci Sequence

Ducci Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by t