ECJTU&江西高校14级新生友谊赛

来源:互联网 发布:毁童年h知世本子图片 编辑:程序博客网 时间:2024/05/16 20:44
ECJTU&江西高校14级新生友谊赛
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71551#overview
A - 1001
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

有N个飞船进行比赛,它们的跑道为直线并互相平行。每个飞船的起跑位置均不相同。第i个飞船从起跑线右边Xi处开始向右行驶(Xi各不相同)。比赛开始后,它能在零时间内加速到最大速度Vi并永远保持此速度。比赛没有终点,即会永远进行下去。

你的任务是算出比赛过程中一共有多少次"超车"。

Input

输入数据由多组数据组成。每组数据格式如下:
第一行为一个整数N(1<=N<=250000)。
接下来的N行,每行两个整数Xi (0≤Xi≤10^6)和Vi(0<Vi<100),描述了一辆飞船的起跑位置和最大速度。
给出的飞船信息按照起跑位置Xi的升序排列,即X1<X2<X3<…<Xn。
最后一组数据N=0,标志输入结束,不需要处理。

Output


对于每组数据,输出仅一行包含一个整数,即"超车"的次数对1000000的模。

Sample Input

40 22 13 86 30

Sample Output

2

归并排序,线段树,树状数组均可解。果断套归并排序模板。各种方法具体原理网上很多,这里不再赘述。下面是AC代码

/*求一个数组的逆序数,用归并排序实现时间复杂度为O(NlogN),提高了单纯使用冒泡排序的速度。只是在归并排序的基础上加了句 result += middle - i + 1;如果后一个数组中的数比前一个数组中的数小,既input[j] < input[i]时,,我们的计数器result就需要增加,而增加的量应该是前一个数组的剩余数据的个数middle-i+1,归并排序的目的是通过把两个前后已经排好序的数组合并排成一个有序的数组。既后面那个数组的第一个数input[j],如果要通过使其有序,那么它得和前面middle-i+1个数字交换位置。需要注意的是result是个很大的值,得用longlong 型类型。*/#include <iostream>#include <stdio.h>#include <stdlib.h>#define MAXN 500005using namespace std;int N;int input[MAXN] = {0};int tmp[MAXN];long long result;void merge(int left, int middle, int right){     int i, j, k;     i = left, j= middle+1, k = 1;     while(i<= middle && j <= right)     {               if(input[j] < input[i])               {                    tmp[k++] = input[j++];                    result += middle - i + 1;        //增加的语句。                    result%=1000000;               }               else                    tmp[k++] = input[i++];     }     while(i <= middle)  tmp[k++] = input[i++];     while(j <= right) tmp[k++] = input[j++];     for(i = left, k = 1; i<= right; i++, k++)            input[i] = tmp[k];}void merge_sort(int left, int right){     if(left < right)     {             int middle = (left + right)/2;             merge_sort(left, middle);             merge_sort(middle+1, right);             merge(left, middle, right);     }}int main(){    int i, j, k;    while(true)    {          cin >> N;          if(N == 0) break;            int y;          for(i = 1; i<= N; i++)              scanf("%d%d",&y, &input[i]);          result = 0;          merge_sort(1, N);          cout << result << endl;    }    return 0;}

B - 1002
Time Limit:9000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu
SubmitStatus

Description

A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Followed by t lines containing integers K.

Output

For each K, output the smallest palindrome larger than K.

Example

Input:

2

808

2133

Output:

818

2222

Warning: large Input/Output data, be careful with certain languages

题意:给你一个数字n,其位数不超过1000000位。
叫你求出比它大的最小的回文数字。

最直接的想法:暴力
每次做大整数加法,并判断是否为回文数字即可。
但是,效率很低。
例如:k=999999999999911111111111,就要做许多次加法运算。
结果TLE
正确解法:构造法
算法:
(1)判断字符串s是否是全9字符串“9999....99”,若是,直接打印“1(len(s)-1个0)1”
(2)将字符串均分为2个部分,中心位置为p,若len为奇数,p=len/2;若len为偶数,p=len/2-1;

3)若前半部分的所有字符s[i]都大于后半部分的相应字符s[len-i-1],直接将前半部分的字符复制到后半部分的相应位置;


(4)若前半部分的字符至少有一个字符小于后半部分的相应字符或是字符串本身就是回文数,则将中间位置的字符p+=1,若有进位,依次向前进位,直到 i=0,最后将前半部分的字符复制到后半部分的相应位置;
下面是AC代码:

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<fstream>#include<vector>#include<map>#include<stack>#include<list>#include<set>#include<queue>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1 | 1using namespace std;const int maxn=1000005,inf=1<<29;int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};char s[maxn];char ans[maxn];int len, ti, tj;bool check(char str[]){    int i;    for(i = 0; i < len; i++)        if(str[i] != '9') return 0;    return 1;}bool judge(char str[]){    int p1, p2;    if(len % 2 == 0) p1 = len / 2 - 1,p2 = len / 2;    else p1 = p2 = len / 2;    ti = p1, tj = p2;    if(len % 2 == 1) p1--,p2++;    while(p1 >= 0)    {        if(str[p1] < str[p2]) return 0;        else if(str[p1] > str[p2]) return 1;        p1--;p2++;    }    return 0;}int main(){    int t, i;    scanf("%d", &t);    while(t--)    {        scanf("%s", s);        len = strlen(s);        int cnt = 0;        if(len == 1 && s[0] < '9') {s[0]++; printf("%s\n", s); continue;}        if(check(s))        {            printf("1");            for(i = 0; i < len - 1; i++) printf("0");            printf("1\n");            continue;        }        if(len % 2 == 0)        {            for(i = 0; i < len / 2; i++) ans[cnt++] = s[i];            for(i = len / 2 - 1 ; i >= 0; i--) ans[cnt++] = s[i];            ans[cnt++] = '\0';        }        else        {            for(i = 0; i <= len / 2; i++) ans[cnt++] = s[i];            for(i = len / 2 - 1; i >= 0; i--) ans[cnt++] = s[i];            ans[cnt++] = '\0';        }        if(judge(s))        {            printf("%s\n", ans);            continue;        }        ans[ti]++;        ans[tj] = ans[ti];        if(ans[ti] > '9')        while(ans[ti] > '9' && ti >= 0)        {            ans[ti] = '0';            ans[tj] = ans[ti];            ti--;            tj++;            ans[ti]++;            ans[tj] = ans[ti];        }        printf("%s\n", ans);    }    return 0;}

C - 1003
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.

There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th(1 ≤ i ≤ k) child wrote the numberai(1 ≤ ai ≤ n·k). All numbersai accidentally turned out to be different.

Now the children wonder, how to divide the orange so as to meet these conditions:

  • each child gets exactly n orange segments;
  • the i-th child gets the segment with numberai for sure;
  • no segment goes to two children simultaneously.

Help the children, divide the orange and fulfill the requirements, described above.

Input

The first line contains two integers n,k(1 ≤ n, k ≤ 30). The second line containsk space-separated integers a1, a2, ..., ak(1 ≤ ai ≤ n·k), where ai is the number of the orange segment that thei-th child would like to get.

It is guaranteed that all numbers ai are distinct.

Output

Print exactly n·k distinct integers. The firstn integers represent the indexes of the segments the first child will get, the secondn integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.

You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

Sample Input

Input
2 24 1
Output
2 4 1 3 
Input
3 12
Output
3 2 1 
题意:共有n*k个不同的数,有k个孩子,每个孩子必须选n个数,而且每两个孩子不能选相同的数。此外,每个孩子所选的n个数当中必须包括a[k]。打印出每个孩子所选的数组,只需要打印出满足条件的一组数就行。
水题。
直接模拟即可。

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<fstream>#include<vector>#include<map>#include<stack>#include<list>#include<set>#include<queue>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1 | 1using namespace std;const int maxn=1000005;int a[maxn],b[maxn];int main(){    int n,k,x;    cin.sync_with_stdio(false);    while(cin>>n>>k)    {        for(int i=1;i<=n*k;i++) a[i]=i;        for(int i=1;i<=k;i++) cin >> x,a[x]=0,b[i]=x;        int cnt=1;        for(int i=1;i<=k;i++)        {            int t=1;            cout<<b[i];            while(t<n)                if(a[cnt]) cout<<" "<<a[cnt++],t++;                else cnt++;            cout<<endl;        }    }    return 0;}

D - 1004
Time Limit:6000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu
SubmitStatus

Description

Shridhar wants to generate some prime numbers for his cryptosystem. Help him!
Your task is to generate all prime numbers between two given numbers.

Input

The first line contains t, the number of test cases (less then or equal to 10).

Followed by t lines which contain two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n,
one number per line. Separate the answers for each test case by an empty line.

Example

Input:21 103 5Output:235735
题意;给出两个数,按顺序输出这两个数所表示区间的所有素数。
素数筛法模板题,两次使用素数筛法即可。

素数筛法这里不再赘述,原理自行用百度谷歌一下。

下面是AC代码。

#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<iostream>using namespace std;const int L=1000005,inf=1<<30,maxn=1005;int prime[L],p[L];bool is[L];int np=0,n,m;void getPrime(){    fill(is,is+L,1);    is[1]=0;    for(int i=2;i<L;i++)        if(is[i])        {            prime[++np]=i;            for(int j=2*i;j<L;j+=i) is[j]=0;        }   // for(int i=1;i<=np;i++)        //cout<<prime[i]<<" ";}void getPrime2(){      fill(p,p+L,0);      for(int i=1;i<=np&&prime[i]<=n;i++)      {           int k=m/prime[i];           for(int j=k;j*prime[i]<=n;j++)           {                if(j>1&&j*prime[i]>=m)p[j*prime[i]-m]=1;           }      }      for(int i=0;i<=n-m;i++)      {        if(!p[i]&&i+m!=1)   printf("%d\n",i+m);      }}int main(){    getPrime();    //cout<<"yes"<<endl;    int t;    cin>>t;    while(t--)    {        cin>>m>>n;       // cout<<"yes"<<endl;        getPrime2();    }    return 0;}
E-1005

恶心模拟题,不写。

F - 1006
Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

Let's denote d(n) as the number of divisors of a positive integern. You are given three integersa, b andc. Your task is to calculate the following sum:

Find the sum modulo 1073741824(230).

Input

The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 100).

Output

Print a single integer — the required sum modulo 1073741824(230).

Sample Input

Input
2 2 2
Output
20
Input
5 6 7
Output
1520
题意: 设d(n)为n的约数的个数
给定a,b,c;求

其中a,b,c均为小于100的正整数。

水题。
思路:枚举+记忆化求值
AC代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<fstream>#include<vector>#include<map>#include<stack>#include<list>#include<set>#include<queue>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1 | 1using namespace std;const int maxn=1000005,inf=1<<29;int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};LL a,b,c;LL d[maxn];LL solve(LL x){    if(d[x]) return d[x];    for(LL i=1;i*i<=x;i++)        if(x%i==0)        {            if(i*i==x) d[x]++;            else d[x]+=2;        }    return d[x];}int main(){    cin.sync_with_stdio(false);    //fill(d,d+maxn,0);    while(cin>>a>>b>>c)    {        LL sum=0;        for(LL i=1;i<=a;i++)            for(LL j=1;j<=b;j++)                for(LL k=1;k<=c;k++)                    sum=(sum+solve(i*j*k))%1073741824;        cout<<sum<<endl;    }    return 0;}

G - 1007
Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops
题意:给定一部字典。即一个单词映射到另外一个单词。
再给一些单词进行查询。若不存在则输出eh
思路:MAP的简单应用。
又是一道大水题。
下面是AC代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<sstream>#include<fstream>#include<vector>#include<map>#include<stack>#include<list>#include<set>#include<queue>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1 | 1using namespace std;const int maxn=100005,inf=1<<29;int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};int n,m,t;int main(){    string s,str;    cin.sync_with_stdio(false);    map<string,string>Map;    while(getline(cin,s))    {        istringstream is(s);        int cnt=0;        while(is>>str) cnt++;        if(cnt==2)        {            istringstream op(s);            string s1,s2;            op>>s1>>s2;            Map[s2]=s1;        }        else if(cnt==1)        {            if(Map.find(str)!=Map.end()) cout<<Map[str]<<endl;            else cout<<"eh"<<endl;        }    }    return 0;}




0 0
原创粉丝点击