首页 > ACM题库 > HDU-杭电 > hdu 1965 SETI-数论-[解题报告]C++
2013
12-26

hdu 1965 SETI-数论-[解题报告]C++

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� 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&#8722;1 the function 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 �?.�?and ??(asterisk). No string will be longer than 70 characters.

输出:

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 �?.�?and ??(asterisk). No string will be longer than 70 characters.

样例输入:

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>

using namespace std;

#define N 100

int P,n;

int matrix[N][N],ans[N];

char str[N];

void init()

{

    int i,j,k;

    for(i = 0; i < n; i++)

        if(str[i] == '*')

            matrix[i][n] = 0;

        else matrix[i][n] = str[i] - 'a' + 1;



    for(i = 0; i < n; i++)

    {

        k = 1;

        for(j = 0; j < n; j++)

        {

            matrix[i][j] = k;

            k = (k*(i+1))%P;

        }

    }

}

int find(int a, int y,int P=100000)//如果方程中不需要取模则直接传入前两个参数

{

    int i;

    a = (a%P + P) % P; y = (y%P + P)%P;

    for(i = 0; i < P; i++)

        if((a*i)%P == y)

            return i;

}

void gauss(int P=100000)//如果方程中不需要取模则不加参数

{

    int i,j,k;

    int row,col;

    int a,b,c,sum;

    row = 0; col = 0;

    while(row < n && col < n)

    {

        for(i = row, j = -1; i < n; i++)

            if(matrix[i][col] != 0)

            {

                j = i; break;

            }

        for(i = col; i < n; i++)

        {

            k = matrix[row][i];

            matrix[row][i] = matrix[j][i];

            matrix[j][i] = k;

        }

        a = matrix[row][col];

        for(i = row+1; i < n; i++)

        {

            b = matrix[i][col];

            for(j = col; j <= n; j++)

                matrix[i][j] = (matrix[i][j]*a - matrix[row][j]*b)%P;

        }

        row++; col++;

    }

    for(i = n-1; i >= 0; i--)

    {

        sum = 0;

        for(j = i+1; j < n; j++)

            sum = (sum + matrix[i][j]*ans[j])%P;

        ans[i] = find(matrix[i][i],matrix[i][n]-sum,P);

    }

    for(i = 0; i < n; i++)

    {

        printf("%d",ans[i]);

        if(i == n-1) printf("\n");

        else printf(" ");

    }

}

int main()

{

    int i,j,k;

    int cases;

    scanf("%d",&cases);

    while(cases--)

    {

        scanf("%d %s",&P,str);

        n = strlen(str);

        init();

        gauss(P);

    }

    return 0;

}

解题转自:http://blog.csdn.net/wwwzys/article/details/6716484


  1. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }