zoukankan      html  css  js  c++  java
  • POJ#2065. SETI

    题目描述

    For some years, quite a lot of work has been put into listening to electromagnetic radio signals received from space, in order to understand what civilizations in distant galaxies might be trying to tell us. One signal source that has been of particular interest to the scientists at Universit´e de Technologie Spatiale is the Nebula Stupidicus. 
    Recently, it was discovered that if each message is assumed to be transmitted as a sequence of integers a0, a1, ...an-1 the function f (k) = ∑0<=i<=n-1aiki (mod p) always evaluates to values 0 <= f (k) <= 26 for 1 <= k <= n, provided that the correct value of p is used. n is of course the length of the transmitted message, and the ai denote integers such that 0 <= ai < p. p is a prime number that is guaranteed to be larger than n as well as larger than 26. It is, however, known to never exceed 30 000. 
    These relationships altogether have been considered too peculiar for being pure coincidences, which calls for further investigation. 
    The linguists at the faculty of Langues et Cultures Extraterrestres transcribe these messages to strings in the English alphabet to make the messages easier to handle while trying to interpret their meanings. The transcription procedure simply assigns the letters a..z to the values 1..26 that f (k) might evaluate to, such that 1 = a, 2 = b etc. The value 0 is transcribed to '*' (an asterisk). While transcribing messages, the linguists simply loop from k = 1 to n, and append the character corresponding to the value of f (k) at the end of the string. 
    The backward transcription procedure, has however, turned out to be too complex for the linguists to handle by themselves. You are therefore assigned the task of writing a program that converts a set of strings to their corresponding Extra Terrestial number sequences.

    输入格式

    On the first line of the input there is a single positive integer N, telling the number of test cases to follow. Each case consists of one line containing the value of p to use during the transcription of the string, followed by the actual string to be transcribed. The only allowed characters in the string are the lower case letters 'a'..'z' and '*' (asterisk). No string will be longer than 70 characters.

    输出格式

    For each transcribed string, output a line with the corresponding list of integers, separated by space, with each integer given in the order of ascending values of i.

    样例输入输出

    输入

    3
    31 aaa
    37 abc
    29 hello*earth

    输出

    1 0 0
    0 1 0
    8 13 9 13 4 27 18 10 12 24 15

    最近啦,再练高斯消元的题 , 算是比较简单的高斯消元 。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    //#include <cmath>
    const long long inf = 1152921504606846976 ;
    const int Inf = 1<<30 , maxn = 81 ;
    using namespace std ;
    int  t , p , n ;
    char s[maxn] ;
    long long a[maxn][maxn] , x[maxn] ;
    bool free_x[maxn] ; 
    
    
    void Init( ) 
    {
        scanf( "%d%s" , &p , s ) ;
        n = strlen( s ) ;
        for( int i = 0 ; i < n ; ++i )
        {
            if( s[i] != '*' )
            a[i][n] = ( s[i] - 'a' + 1 ) % p ; a[i][0] = 1 ; // n -> he 
            for( int j = 1 ; j < n ; ++j )  a[i][j] = (a[i][j-1] * ( i + 1 ) ) % p ; // xi shu
        }
    }
    
    long long int qabs( long long a )
    {
         if( a > 0 ) return a ;
         return -a ;
    }
    
    
    void Solve( )
    {
    //    max_r = 0 ; 
        int k , col ; 
        for( k = 0 , col = 0 ; k < n && col < n ; ++k , ++col )
        {
            int max_r = k ; 
            for( int i = k + 1 ; i < n ; ++i )    
                if( qabs(a[i][col]) > qabs( a[max_r][col] ) ) max_r = i ; // find biggest and change
            if( max_r != k ) //and change
            {
                for( int i = k ; i < n + 1 ; ++i )
                    swap( a[k][i] , a[max_r][i] ) ;
            }
            for( int i = k + 1 ; i < n ; ++i )
            {
                if( a[i][col] == 0 ) continue ;
                long long x1 = a[i][col] , x2 = a[k][col] ; 
                for( int j = col ; j < n + 1 ; ++j )
                {
                    a[i][j] = a[i][j] * x2 - a[k][j] * x1 ; 
                    a[i][j] = ( ( a[i][j] % p + p ) % p + p ) ; 
                }
            }
        }
        for( int i = k - 1 ; i >= 0 ; --i )
        {
            long long tmp = a[i][n] ; 
            for( int j = i + 1 ; j < n ; ++j )
                tmp = ( ( tmp - a[i][j] * x[j] ) % p + p ) % p ;
            while( tmp % a[i][i] ) tmp += p ;
            x[i] = ( ( tmp/ a[i][i] ) %p + p ) %p ;
        }
    }
    
    
    void Output( )
    {
        for( int i = 0 ; i < n ; ++i )
        {
            if( i != 0 ) printf( " " ) ;
            printf( "%lld" , x[i] ) ;
        }
            
        printf( "
    " ) ;
    }
    
    
    
    int main( )
    {
    //    freopen( "POJ#2065.in" , "r" , stdin ) ;
    //    freopen( "POJ#2065.out"  , "w" , stdout ) ;
        scanf( "%d" , &t ) ;
        while( t-- )
        {
            memset( a , 0 , sizeof(a) ) ;
            memset( x , 0 , sizeof(x) ) ;
            memset( free_x , 0 , sizeof(free_x) ) ;
            Init( ) ;
            Solve( ) ;
            Output( ) ;
        }
        fclose( stdin ) ;
        fclose( stdout ) ;
        return 0 ;
    }




     
  • 相关阅读:
    十、Compose命令
    九、compose入门
    JS数组中,两两比较的算法,为了获取重复的值,并在php数组中,使用这种倒三角算法
    题目重复度检测---四种相似度检测的方案+PHP改进计算字符串相似度的函数similar_text()、levenshtein()
    js让文字一个个出现
    客户总想让页面炫酷起来-----怎么炫酷呢?---使用css3的东西或者其animate.css
    关于git的总结:git知识大全,命令行操作,vscode操作
    defer和async的区别
    dom和bom的区别,以及三类偏移属性(JS运动方法中,经常会涉及到这些位置信息)
    认真的对待flex,Flex模型和Flex属性思维导图,以及自己的记忆方法--jaf和aof
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5998232.html
Copyright © 2011-2022 走看看