DAY_7-ACM Weekly Summary (1)

Source: Internet
Author: User
Tags bitset gcd

A: Interval or value topic:
gbx从老板那里获得了一个长度为n的非负整数序列,以及老板的一个需求。老板要gbx写一个程序支持查询一个区间的or的结果。当老板给出一个区间[x,y]的时候,gbx就必须立刻告诉老板A[x] or A[x+1] or A[x+2] or … or A[y]的结果。现在老板有m次询问,gbx觉得太烦,就要你来解决这个问题。
Input format:
    第一行一个整数T,表示数据的组数(1<=T<=5)    对于每组数据,首先读入两个整数n,m ( 1<=n,m<=10^5)表示序列长度和老板询问次数。    接下来一行读入n个非负整数,表示序列的数,每个数不超过10^9。    接下来m行,每行两个整数xi,yi,表示询问区间(1<=x<=y<=n)。
Output format:
对于每次询问,输出一个整数,代表询问区间的or和。
Input Sample:
16 31 2 3 4 5 61 33 52 4
Sample output:
377
Attention:
or是二进制按位取或操作(符号是一个竖线|)。4 or 6 = (100) or (110)=(110)=6
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <iostream>#include <algorithm>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <bitset>using namespace STD;Const intmaxn=100000+ -;intn,m,a[maxn][ -],sum[maxn][ -];intMainintargcChar* argv[]) {intzz=0;scanf("%d", &zz); for(inttest=1; test<=zz; test++)//while (zz-){scanf("%d%d", &n,&m); for(intI=1; i<=n; i++)input acquisition of//n data{intXscanf("%d", &x); for(intj=0; j< to; J + +) {a[i][j]= (x>>j) &1;//Split each number of bits per data, binary, sorted by low to high order                //printf ("%d", a[i][j]);}//printf ("#1 \ n");} for(intj=0; j< to; J + +) {sum[0][j]=0;//sum The initialization of two-dimensional arrays            //printf ("%d", sum[0][j]);}//printf ("&\n");         for(intI=1; i<=n; i++) { for(intj=0; j< to; J + +) {sum[i][j]=sum[i-1][J]+A[I][J];//Add the broken data in a bitwise               //printf ("%d", sum[i][j]);}//printf ("#2 \ n");} while(m--)//m Times Inquiry{intx, y;scanf("%d%d", &x,&y);//Ask scope data range            intans=0; for(intj=0; j< to; J + +) {if(sum[y][j]-sum[x-1][j]>0)//from x-1 to Y-bit data, there are 1 occurrences of this bit{//printf ("%d%d\n", sum[y][j],sum[x-1][j]);Ans|= (1&LT;&LT;J);//(1<<J) Gets the value of the decimal representation of the bit data                   //printf ("(1<<j) =%d ans=%d\n", 1<<j,ans);}            }printf("%d\n", ans); }    }return 0;}
Summarize

In the face of this problem, I'm a fool to build an array to store the data, and then loop [x, y] inside the ' | ' operation, but you have not considered the time limit problem. Data range is 10^5, each data is 32 bits, run reached 2800ms, severe timeout.

C: The most cute height difference problem:
   fq进入了ccnu神奇的acm实验室,出去比赛的时候拍合照。fq观察照片发现大家都从左到右站成一排,更神奇的是:他发现实验室队员里出现了最萌身高差。于是,fq翻出了更多的黑历史,想找出每张照片的最萌身高差。   每张照片,有n个人,所有人从左到右排成一排,身高差定义为相邻两个人的身高差的绝对值,最萌身高差就是指所有身高差最大的那个。
Input format:
    第一行一个整数T,表示T组数据。    每组数据首先有一行一个整数,表示人数n(1<=n<=10^4)。    接下来n行,每行一个字符串和一个整数,表示该位置的人的名字和身高。字符串长度不超过20,整数不超过300
Output format:
    对于每组数据,输出两行。    第一行输出一个整数,表示最萌身高差是多少。     第二行输出两个字符串(中间用一个空格隔开),表示最萌身高差的两个人是谁(按从左到右输出,如果有多个,输出最左边的两个人)
Input Sample:
16wh 175fq 165wq 170gbx 165xb 190ooc 175
Sample output:
25gbx xb
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <iostream>#include <algorithm>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <bitset>using namespace STD;Const intmaxn=10000+ -;intN,A[MAXN];Charname[maxn][ -];intMainintargcChar* argv[]) {intzz=0;scanf("%d", &zz); for(inttest=1; test<=zz; test++) {scanf("%d", &n); for(intI=1; i<=n; i++)scanf("%s%d", Name[i],&a[i]);intans=-1K for(intI=1; i<n; i++) {intret=ABS(a[i]-a[i+1]);if(Ret>ans) ans=ret,k=i; }printf("%d\n", ans);printf("%s%s\n", name[k],name[k+1]); }return 0;}
Summarize

This is a simple question, I can make it, the test is the input and output string, and then a simple search by order.

D:GCD and LCM Topics:
   gbn在教小朋友数论,他在黑板上写了一个题:给出两个正整数x和y,其中gcd(a,b)=x,lcm(a,b)=y,问有多少对满足条件的正整数解(a,b)?   gcd是两个数的最大公约数,lcm是两个数的最小公倍数。
Input format:
    第一行一个整数T,表示数据组数。(T<=20)    接下来T行,每行两个正整数,表示x和y。(1<=x , y<=10^6)
Output format:
    对于每一个数据,输出一行一个整数g,表示有g对(a,b)满足条件。
Input Sample:
31 123 122 24
Sample output:
424
Code:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <iostream>#include <algorithm>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <bitset>using namespace STD;#define BLL Long Long#define DOU Double#define FOR (I,A,B) for (int i= (a), _# #i = (b); i<=_# #i; i++)#define ROF (I,A,B) for (int i= (a), _# #i = (b); i>=_# #i; i--)#define REP (i,a,b) for (int i= (a), _# #i = (b); i<=_# #i; i++)#define REK (I,A,B) for (int i= (a), _# #i = (b); i>=_# #i; i--)#define MEM (A, B) memset (A,b,sizeof (a) )#define CPY (A, B) memcpy (a,b,sizeof )intgcdintAintb) {if(b==0)returnAreturnGCD (b,a%b);}intMainintargcChar* argv[]) {intzz=0;scanf("%d", &zz); for(inttest=1; test<=zz; test++) {intx, y;scanf("%d%d", &x,&y);if(y%x!=0)//Common factor and common multiple coprime{printf("0\n");Continue; }intr=y/x;//a,b Two number are removed common factor, the smaller number of the range is less than sqrt (r), or less than Max (K1,K2) *x;        intans=0; for(intI=1; i*i<=r; i++)if(r%i==0&& gcd (i,r/i) = =1)//i is the factor of r, while I and r/i coprime{ans++;if(i!=r/i)//a!=b, the data position can be reversedans++; }printf("%d\n", ans); }return 0;}
Summarize:

X=GCD (A, b);
Y=LCM (A, B) =a*b/gcd (A, b);
X * Y=a * b;
A=K1 * X,B=K2 * x (K1,k2 coprime,);
Y=K1 * K2 * x;
Their own stupid method of course over time, the data range is 10^6, oneself also has not found the scope of the loop isi*i<=r;

E: Flames topic:
    一天WH睡醒之后发现自己竟然穿越到了一个战火纷飞的朝代,而且他还是一个国君,尽管如此不可思议,但是WH内心毫无波动,甚至还想睡觉。这时候一个御用太监递上了一份册子,WH定睛一看,这是现在各个国家的战斗力表,WH知道,只要战斗力比自己低或者相等的国家就能打败他们,而且如果现在他去攻打一个国家战斗力只有自己国家的一半或者一半以下的国家,那么他将毫不费力地拿下这个国家,并且被战败的这个国家的战斗力将会全部加到自己国家上,但是如果他去攻打一个国家战斗力超过自己国家战斗力一半的国家,打败这个国家之后,自己国家的战斗力会增加上战败国战斗力的一半,现在由于WH刚睡醒所以不想看这个战斗力表,所以他想把这个战斗力表给你,让你算算他最终能不能打败所有国家,如果能输出他的国家最终能达到的最高的战斗力,如果不能,输出-1。(文中所有的除法计算若不能整除,则向下取整)    国家的数目n的范围为[1, 10000], 该战斗力表上所有国家的战斗力范围都在[1,INT_MAX]内。    注意:战斗力相等的国家也可以打赢。
Input:
多组数据。每组数据的第一行为一个数字n,表示有n个国家。接下来的一行有n个数字,代表n个国家的战斗力,其中第一个数为WH所在国家的战斗力。
Output:
对于每组数据,输出一个数:若WH能够打败所有国家则输出他的国家能达到的最大的国家战斗力,如果不能输出-1。
Sample Input
59 2 10 4 1355 9 8 14 2
Sample Output
26-1
Hint:
第一组样例解释:先把战斗力为4的国家打败,自己国家的战斗力上升为13,再把战斗力为2的国家打败,自己国家的战斗力上升为15,再把战斗力为10的国家打败,自己国家的战斗力上升为20,再把战斗力为13的国家打败,自己国家的战斗力上升为26第二组样例解释:先把战斗力为2的国家打败,自己国家的战斗力上升为7,无法继续打败其他国家。
Summarize:

The personal idea is, first of all, the national war power values ordered, sequentially, know to play or not win. Of course, how simple it is. The world slowly malicious tell me, when encountered 10 6 8 this situation, first hit 8 better, and then thought should be after each battle force value half and a bit after the choice of the highest benefit (hit 8 get 4, dozen 6 to get 3), but the world of Evil told me, hit the 20 12 13 first 12 better, If there is an expression here, I really want to hide my face and cry 、、、 and then stupid I don't know what to do,

DAY_7-ACM Weekly Summary (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.