hdu 1930 中国剩余定理(互质模板题)

And Now, a Remainder from Our Sponsor

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 13

Problem Description

IBM has decided that all messages sent to and from teams competing in the ACM programming contest should be encoded. They have decided that instead of sending the letters of a message, they will transmit their remainders relative to some secret keys which are four, two-digit integers that are pairwise relatively prime. For example, consider the message "THE CAT IN THE HAT". The letters of this message are first converted into numeric equivalents, where A=01, B=02, ..., Z=26 and a blank=27. Each group of 3 letters is then combined to create a 6 digit number. (If the last group does not contain 3 letters it is padded on the right with blanks and then transformed into a 6 digit number.) For example 
THE CAT IN THE HAT → 200805 270301 202709 142720 080527 080120
Each six-digit integer is then encoded by replacing it with the remainders modulo the secret keys as follows: Each remainder should be padded with leading 0’s, if necessary, to make it two digits long. After this, the remainders are concatenated together and then any leading 0’s are removed. For example, if the secret keys are 34, 81, 65, and 43, then the first integer 200805 would have remainders 1, 6, 20 and 38. Following the rules above, these combine to get the encoding 1062038. The entire sample message above would be encoded as
1062038 1043103 1473907 22794503 15135731 16114011

Input

The input consists of multiple test cases. The first line of input consists of a single positive integer n indicating the number of test cases. The next 2n lines of the input consist of the test cases. The first line of each test case contains a positive integer (< 50) giving the number of groups in the encoded message. The second line of each test case consists of the four keys followed by the encoded message. Each message group is separated with a space.

Output

For each test case write the decoded message. You should not print any trailing blanks.

Sample Input

2
6
34 81 65 43 1062038 1043103 1473907 22794503 15135731 16114011
3
20 31 53 39 5184133 14080210 7090922

Sample Output

THE CAT IN THE HAT
THE END

Source

2007 East Central North America

#include<bits/stdc++.h>
using namespace std;
const int N=100;
int m[5],aa[N],a[N][10],ans[N];
char s[1000];int cnt;
int exgcd(int a, int b, int &x, int &y){
    int d = a;
    if(b != 0){
        d = exgcd(b, a%b, y, x);
        y -= (a / b) * x;
    }
    else{
        x = 1; y = 0;
    }
    return d;
}
int China(int n)
{
    int M = 1,ans = 0;
    for(int i = 1; i <=4; i++)  M *= m[i];
    for(int i=1; i<=4; i++){
        int x,y,Mi = M / m[i];
        exgcd(Mi, m[i], x, y);
        ans = (ans + Mi * x * a[n][i]) % M;
    }
    if(ans < 0) ans += M;
    return ans;
}
void change(int n){
    for(int i=1;i<=n;i++){
        for(int j=4;j>=1;j--){
            a[i][j]=aa[i]%100;
            aa[i]/=100;
        }
    }
}
void solve(int n){
    for(int i=1;i<=n;i++) ans[i]=China(i);
    //for(int i=1;i<=n;i++) cout<<ans[i]<<endl;
    for(int i=1;i<=n;i++){
        for(int j=3;j>=1;j--){
            int tmp=ans[i]%100;
            if(tmp==27) s[cnt+j]=‘ ‘;
            else s[cnt+j]=‘A‘-1+tmp;
            ans[i]/=100;
        }
        cnt+=3;
    }
    while(s[cnt]==‘ ‘) cnt--;
    for(int i=1;i<=cnt;i++) printf("%c",s[i]);
    printf("\n");
}
int main(){
    int t,n;scanf("%d",&t);
    while(t--){
        scanf("%d",&n);cnt=0;
        for(int i=1;i<=4;i++) scanf("%d",&m[i]);
        for(int i=1;i<=n;i++) scanf("%d",&aa[i]);
        change(n);
        solve(n);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/vainglory/p/9185408.html

时间: 2024-10-09 11:06:38

hdu 1930 中国剩余定理(互质模板题)的相关文章

中国剩余定理 互质与非互质版本

中国剩余定理互质版 设m1,m2,m3,...,mk是两两互素的正整数,即gcd(mi,mj)=1,i!=j,i,j=1,2,3,...,k. 则同余方程组: x = a1 (mod n1) x = a2 (mod n2) ... x = ak (mod nk) 模[n1,n2,...nk]有唯一解,即在[n1,n2,...,nk]的意义下,存在唯一的x,满足: x = ai mod [n1,n2,...,nk], i=1,2,3,...,k. 解可以写为这种形式: x = sigma(ai* 

HDU 1370(中国剩余定理)

原文章地址:http://m.blog.csdn.net/article/details?id=18414733 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如, 智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道何时三个高峰落在同一天. 对于每个周期,我们会给出从当前年份的第一

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

hdu 1285 确定比赛名次 (模板题)

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11353    Accepted Submission(s): 4525 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接

hdu 4828 Xor Sum (trie 树模板题,经典应用)

hdu 4825 题目链接 题意:给定n个数,然后给出m个询问,每组询问一个数x,问n中的数y使得x和y的异或和最大. 思路:字典树..把每个数转化成二进制,注意补全前导0,使得所有数都有相同的位数. 如果想要异或和最大,那么每一位尽可能都是1. 所以做法是,先构建字典树,然后每次find的时候,尽可能按照和当前寻找的数的位相反的位的方向走(如果有的话) 比如当前位是1,那我就往0的方向走. 需要注意的是,多组数据,每次要重新初始化一遍. 做法是 在struct 中重新 root = new N

BestCoder Round #80 1004 hdu 5668 中国剩余定理(m不互质版)

链接:戳这里 Circle Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 65536/65536 K (Java/Others) 问题描述 \ \ \ \     Fye对约瑟夫游戏十分着迷. \ \ \ \     她找到了n个同学,把他们围成一个圈,让他们做约瑟夫游戏,然后她得到了一个同学们出圈的编号序列.游戏是这样进行的:以同学1为起点,开始计数,计数到第k个同学,该同学出圈.出圈的同学将不参与之后的计数. \ \ \ \  

【网络流#2】hdu 1533 最小费用最大流模板题

嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(House)作为汇点,各个源点与汇点分别连一条边,这条边的流量是1(因为每个源点只能走一条边到汇点),费用是 从源点走到汇点的步数,因为有多个源点与汇点,要建一个超级源点与超级汇点,超级源点与各个源点连一条流量为1,费用为0(要避免产生多余的费用)的边 按照这个图跑一发费用流即可 把代码挂上去,用的是前向星写的 1 #include<cstdio> 2 #include<cstr

HDU 5446 中国剩余定理+lucas

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2389    Accepted Submission(s): 885 Problem Description On the way to the next secret treasure hiding place, the mathematician

HDU 2222 Keywords Search(AC自动机模板题)

原题大意:原题链接 先给定T个单词,然后给定一个字符串,查询该字符串中包含多少个给定的单词 解题思路:AC自动机模板题 参考链接:哔哩哔哩算法讲堂 WA版本 注意:因为输入的单词可能有重复,那么Insert()函数中p->id=id;语句中p->id会被覆盖,在Query()函数中会一次性全部被清零,导致不能查询重复单词,以至于结果res错误. #include<queue> #include<cstdio> #include<cstring> using