[historical legacy] there are also records: HDOJ - brush questions md

HDOJ travel

[by_041]

There was once a preface

catalogue

ACM Steps

Chapter One - phase 1

Section One - basic input and output

It is a summary of the input and output of the classic ACM competition

    • P1089: multiple groups of data, one group occupies one line, with two numbers until the end of EOF
    • While (CIN > > a > > b) or while (~scanf ('% d% d', & A, & B))
    • P1090: the first row is an n, followed by n groups of data. One group occupies one row, with two numbers
      • Direct: input and loop control
    • P1091: multiple groups of data, one group occupies one line, with two numbers until the end of 0 (no processing)
      • While (CIN > > a > > b, a! = "0" |b! = "0") or while (scanf ("% d% d", & A, & B), a|b)
    • P1092: multiple groups of data, one row for each group. The first number of each row is n, followed by N data until n=0 (no processing)
      • while(scanf("%d",&n),n)
    • P1093: group T data, each group has one row of data, the first number of each row is n, followed by N data
      • Direct: input and loop control
    • P1094: multiple groups of data, one line for each group. The first number of each line is n, followed by N data until the end of EOF
      • while(~scanf("%d",&n))
    • P1095: P1089, followed by a blank line = > Add '\ n' directly after each group!!! (note)
      • putchar('\n') is added directly after each group;
    • P1096: P1093, plus a blank line between outputs = > Add '\ n' between two groups of valid data!!! (note)
      • In addition to the first group, the first line of each group is wrapped: for (int CAS = 1; CAS < = t; CAS + +) and the head of each group plus if(cas^1)putchar('\n');
      • Add if(T)putchar('\n') at the end of while(T--)and each group, except for the last group, which wrap after each group;
  • Its common head is the high-precision calculation part of a+b:

    #include<iostream>
    #include<string>
    #include<algorithm>
    
    using namespace std;
    
    string add(string a,string b)
    {
    	if(a.size()<b.size())swap(a,b);
    	reverse(a.begin(),a.end());
    	reverse(b.begin(),b.end());
    	int i,ii=b.size();
    	for(i=0;i<ii;i++)
    	{
    		a[i]+=b[i]-'0';
    		if(a[i]>'9')
    		{
    			a[i]-=10;
    			a[i+1]++;
    		}
    	}
    	for(ii=a.size();i<ii;i++)
    	{
    		if(a[i]>'9')
    		{
    			a[i]-=10;
    			a[i+1]++;
    		}
    	}
    	if(a[ii]==1)
    		a+='1';
    	reverse(a.begin(),a.end());
    	return a;
    }
    

Section Two - simple formula

Formula questions that can directly deduce the answers from the data in the questions are divided into blocks similar to the primary school Olympiad of noi

  • 1.2.1.Climbing Worm(P1049):
  • When a snail climbs a well in Primary School Mathematical Olympiad, it should be noted that the last climb has the following characteristics:
    -The starting height is a multiple of u-d, and the starting height plus u is greater than or equal to the well height n
    • So we can get the formula: a n s s = ( ( n − u ) / ( u − d ) + b o o l ( ( n − u ) % ( u − d ) ) ) ∗ 2 + 1 anss=((n-u)/(u-d)+bool((n-u)\%(u-d)))*2+1 anss=((n−u)/(u−d)+bool((n−u)%(u−d)))∗2+1
#include<iostream>

using namespace std;



int main()
{
    int n,u,d;
    while(scanf("%d%d%d",&n,&u,&d),n|u|d)
    {
        printf("%d\n",((n-u)/(u-d)+bool((n-u)%(u-d)))*2+1);
    }
    return 0;
}
  • 1.2.2.Nasty Hacks(P2317):
    • The difference between the first number and the second number minus the third number can be output
#include<iostream>

using namespace std;

int main()
{
    int n,r,e,c;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d",&r,&e,&c);
        c=r-e+c;//Indicates the advantage of not joining advertising
        if(c)
            if(c>0)
                puts("do not advertise");
            else
                puts("advertise");
        else
            puts("does not matter");
    }
    return 0;
}

  • 1.2.3.find your present (2)(P2095):

    • Find the only number that appears odd times in n numbers

      • Method 1: sort and scan it again
      • Method 2: count with map, and then output odd numbers
      • Method 3: use the linked list method to create a new node at the corresponding position after an odd number of occurrences, and delete the existing node after an even number of occurrences

      The code here uses method 2

#include<iostream>
#include<map>

using namespace std;

int input()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')
		ret=(ret<<1)+(ret<<3)+ch-'0';
	return ret;
}

int main()
{
	int n;
	map<int,int>counter;
	map<int,int>::iterator counter_i;
	while((n=input()))
	{
		counter.clear();
		for(int i=1;i<=n;i++)
			counter[input()]++;
		for(counter_i=counter.begin();counter_i!=counter.end();counter_i++)
		{
			if(counter_i->second&1)
			{
				printf("%d\n",counter_i->first);
				break;
			}
		}
	}
	return 0;
}

  • 1.2.4.Rightmost Digit(P1061):

    • In the case of N-th order, only the bottom ten digits of the answer are considered, so the answer is only one of the following:
      • 0 (there are 1 kinds): all are 0
      • 1 (1): all 1
      • 2(4): 2,4,8,6,2. . loop
      • 3(4): 3,9,7,1,3. . loop
      • 4(2): 4,6,4. . . loop
      • 5 (1): all 5
      • 6 (1): all 6
      • 7(4): 7,9,3,1,7. . loop
      • 8(4): 8,4,2,6,8. . loop
      • 9(2): 9,1,9. . loop
    • Among them, because its index is also n, the answer can only be the bold part above, and only 2, 3, 7 and 8 need to be classified
    • be careful!! 0 0 = 1 0^0=1 00=1!!!
    #include<iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    	int T,n;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		switch(n%10)
    		{
    			case 0:puts(n?"0":"1");break;
    			case 1:puts("1");break;
    			case 2:
    				switch(n%4)
    				{
    					case 2:puts("4");break;
    					case 0:puts("6");break;
    				}
    				break;
    			case 3:
    				switch(n%4)
    				{
    					case 1:puts("3");break;
    					case 2:puts("9");break;
    					case 3:puts("7");break;
    					case 0:puts("1");break;
    				}
    				break;
    			case 4:puts("6");break;
    			case 5:puts("5");break;
    			case 6:puts("6");break;
    			case 7:
    				switch(n%4)
    				{
    					case 1:puts("7");break;
    					case 2:puts("9");break;
    					case 3:puts("3");break;
    					case 0:puts("1");break;
    				}
    				break;
    			case 8:
    				switch(n%4)
    				{
    					case 2:puts("4");break;
    					case 0:puts("6");break;
    				}
    				break;
    			case 9:puts("9");break;
    		}
    	}
    	return 0;
    }
    
  • 1.2.5.The Seven Percent Solution(P2719):

    • You can process the string characters online. If you encounter special characters, output the converted results. Otherwise, directly output the current characters until you encounter the end of '#'
    #include<iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    	char ch;
    	while((ch=getchar())^'#')
    	{
    		if(ch==' ')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('0');
    			continue;
    		}
    		if(ch=='!')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('1');
    			continue;
    		}
    		if(ch=='$')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('4');
    			continue;
    		}
    		if(ch=='%')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('5');
    			continue;
    		}
    		if(ch=='(')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('8');
    			continue;
    		}
    		if(ch==')')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('9');
    			continue;
    		}
    		if(ch=='*')
    		{
    			putchar('%');
    			putchar('2');
    			putchar('a');
    			continue;
    		}
    		putchar(ch);
    	}
    	return 0;
    }
    
  • 1.2.6.Just A Triangle(P3188):

    • Triangle shape judgment: if it is right triangle output 'good', it is isosceles triangle output 'perfect', otherwise it will output 'just a triangle'
    • Solution: first sort the edges by length, judge the right angle and isosceles, and output anss for each case
    #include<iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    	int n,a,b,c;
    	scanf("%d",&n);
    	while(n--)
    	{
    		scanf("%d%d%d",&a,&b,&c);
    		if(a>b)
    			swap(a,b);
    		if(b>c)
    			swap(b,c);
    		if(a>b)
    			swap(a,b);
    		if(a==b||b==c)
    		{
    			puts("perfect");
    			continue;
    		}
    		if(a*a+b*b==c*c)
    		{
    			puts("good");
    			continue;
    		}
    		puts("just a triangle");
    	}
    	return 0;
    }
    
  • 1.2.7.IBM Minus One(P1328):

    • String conversion, which converts each letter to the next digit of his alphabet
    • In particular, convert 'Z' to 'A', and then output in A certain format (acm classic output format)
    • Print a blank line after each test case.
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    
    
    int main()
    {
    	int n;
    	string str;
    	scanf("%d\n",&n);
    	for(int cas=1;cas<=n;cas++)
    	{
    		printf("String #%d\n",cas);
    		cin>>str;
    		for(auto it:str)
    			putchar(it^'Z'?it+1:'A');
    		putchar('\n');
    		putchar('\n');
    	}
    	return 0;
    }
    
  • 1.2.8.Lowest Bit(P1196):

    • Method 1: input a decimal number and output the position of the lowest 1 after converting it into binary number
      • anss=1;while(n&1) {anss++;n>>=1;}anss=1<<anss;
    • Method 2: there is a formula to take this digit directly: (~ n + 1) \ & n
    #include<iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    	int n;
    	while(scanf("%d",&n),n)
    	{
    		printf("%d\n",(~n+1)&n);
    	}
    	return 0;
    }
    

Section Three - preliminary data structure and algorithm

**Preliminary data structure: * * proficient in STL, struct(class) and string processing

**Preliminary algorithm: * * sorting, greedy

  • 1.3.1.FatMouse' Trade(P1009):

    • Greedy, give priority to JavaBean s in the room with the highest exchange efficiency
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct room_
    {
    	double j,f;
    	room_(){}
    	room_(int a,int b):
    		j(a),f(b){}
    	double rate()
    	{return j/f;}
    	room_ input()
    	{
    		scanf("%lf%lf",&j,&f);
    		return *this;
    	}
    };
    bool operator<(room_ a,room_ b)
    {return a.rate()<b.rate();}
    
    
    int main()
    {
    	double n,m,anss;
    	vector<room_>v;
    	while(scanf("%lf%lf",&m,&n),n!=-1||m!=-1)
    	{
    		v.clear();
    		anss=0;
    		for(int i=0;i<n;i++)
    			v.push_back(room_().input());
    		sort(v.begin(),v.end());
    		// for(int i=0;i<n;i++)
    		// 	cout<<v[i].j<<'\t'<<v[i].f<<endl;
    		while(m&&!v.empty())
    		{
    			// cout<<v.back().j<<'\t'<<v.back().f<<endl;
    			if(m>=v.back().f)
    			{
    				anss+=v.back().j;
    				m-=v.back().f;
    			}
    			else
    			{
    				anss+=v.back().j*m/v.back().f;
    				break;
    			}
    			v.pop_back();
    		}
    		printf("%.3lf\n",anss);
    	}
    	return 0;
    }
    
  • 1.3.2. Baibu Chuanyang (P2550):

    • Use * * pair < int, int > * * to store the length and number of arrows
    • Sort by length, and then output in this order
    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    
    using namespace std;
    
    int input()
    {
    	char ch;
    	while((ch=getchar())<'0'||ch>'9');
    	int ret=ch-'0';
    	while((ch=getchar())>='0'&&ch<='9')
    		ret=(ret<<1)+(ret<<3)+ch-'0';
    	return ret;
    }
    
    bool cmp(pair<int,int>a,pair<int,int>b)
    {return a.first<b.first;}//sort order
    
    void she(int a,int b)//Archery Lala
    {
    	string str=">+";
    	for(int i=a-2;i>0;i--)
    		str+="-";
    	str+="+>\n";
    	while(b--)
    		cout<<str;
    	putchar('\n');
    }
    
    
    
    int main()
    {
    	int T,n;
        vector<pair<int,int>>v;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		v.clear();
    		for(int i=0,tem;i<n;i++)
    			tem=input(),			//Pay attention here when using pair!!
    			v.push_back(pair<int,int>(tem,input()));
    		sort(v.begin(),v.end(),cmp);
    		for(int i=0;i<n;i++)
    			she(v[i].first,v[i].second);
    		// for(auto it:v)			//C++11
    		// 	she(it.first,it.second);
    	}
    	return 0;
    }
    
  • 1.3.3. Door opener and door closer (P1234):

    • Use the custom structure struct tim_, Storage time and its corresponding person name
    • Traverse it, leaving the first person with the smallest timestamp and the second person with the largest timestamp
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    struct tim_
    {
    	string name;
    	int h,m,s;
    	tim_(){};
    	tim_(int a,int b,int c):
    	h(a),m(b),s(c){}
    	operator >(tim_ a)
    	{
    		if(h^a.h)
    			return h>a.h;
    		if(m^a.m)
    			return m>a.m;
    		if(s^a.s)
    			return s>a.s;
    		return false;
    	}
    	operator <(tim_ a)
    	{
    		if(h^a.h)
    			return h<a.h;
    		if(m^a.m)
    			return m<a.m;
    		if(s^a.s)
    			return s<a.s;
    		return false;
    	}
    }ne,op,ed;
    
    int main()
    {
    	int T,n;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		op=tim_(25,0,0);
    		ed=tim_(-1,0,0);
    		while(n--)
    		{
    			cin>>ne.name;
    			scanf("%d:%d:%d",&ne.h,&ne.m,&ne.s);
    			if(ne<op)
    				op=ne;
    			scanf("%d:%d:%d",&ne.h,&ne.m,&ne.s);
    			if(ne>ed)
    				ed=ne;
    		}
    		cout<<op.name<<' '<<ed.name<<endl;
    	}
    	return 0;
    }
    
  • 1.3.4. Second small integer (P2561):

    • It is the second small integer (I heard that there is something called LRU algorithm before)
    #include<iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    	int T,n,val,mi,mii;//The lowest decimal mi, the second decimal mii
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		mi=mii=0x7fffffff;
    		while(n--)
    		{
    			scanf("%d",&val);
    			if(val<=mi)
    			{
    				mii=mi;
    				mi=val;
    				continue;
    			}
    			if(val<mii)
    			{
    				mii=val;
    			}
    		}
    		printf("%d\n",mii);
    	}
    	return 0;
    }
    
  • 1.3.5. Sort (P1106):

    • A simple method is string scanning processing. What I'm trying to solve here is online processing
    #include<iostream>
    #include<queue>
    
    
    using namespace std;
    
    char ch;//ch the next character that has not been processed
    int tin_res=0;
    char input_5()//Enter a valid number
    {
    	while(ch<'0'||ch>'9'||ch=='5')
    		ch=getchar();
    	tin_res=ch-'0';
    	while((ch=getchar())>='0'&&ch<='9'&&ch!='5')
    		tin_res=(tin_res<<1)+(tin_res<<3)+ch-'0';
    	return ch;
    }
    
    
    #define tin_number 	 one 	// Input successful
    #define tin_endl 	 two 	// End of line
    #define tin_endflie 	 three 	// End of file
    int try_input()//Try to enter and return the input result
    {
    	while(ch=='5')
    		ch=getchar();
    	if(ch=='\n')
    		{
    			ch=getchar();
    			return tin_endl;
    		}
    	if(ch==EOF)
    		return tin_endflie;
    	input_5();
    	return tin_number;
    }
    
    priority_queue<int,vector<int>,greater<int>>anss;
    
    void output()//Output and clear the current priority queue (number of stored)
    {
    	while(!anss.empty())
    	{
    		printf("%d",anss.top());
    		anss.pop();
    		if(!anss.empty())
    			putchar(' ');
    	}
    	return;
    }
    
    int main()
    {
    	ch=getchar();
    	while(1)
    	{
    		switch(try_input())
    		{
    			case tin_number:
    				// cout<<tin_res<<endl;
    				anss.push(tin_res);
    				break;
    			case tin_endl:
    				// cout<<"[\\n]"<<endl;
    				output();
    				putchar('\n');
    				break;
    			case tin_endflie:
    				// cout<<"[end]"<<endl;
    				output();
    				return 0;
    		}
    	}
    	return 0;
    }
    
  • 1.3.6. Test ranking (P2093):

    • String processing according to the meaning of the question to get the data of each contestant, and then sort according to the requirements
    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct gamer_
    {
    	string name;
    	int accept,penalty;
    	gamer_():
    		name(""),accept(0),penalty(0){}
    	void output()
    	{
    		// cout<<name<<'\t'<<accept<<'\t'<<penalty<<endl;
    		printf("%-10s %2d %4d\n",name.c_str(),accept,penalty);
    		return;
    	}
    	operator < (gamer_ a)
    	{
    		if(accept^a.accept)
    			return accept>a.accept;
    		if(penalty^a.penalty)
    			return penalty<a.penalty;
    		return name<a.name;
    	}
    };
    
    int main()
    {
    	int n,m,player=-1;
    	vector<gamer_>v;
    	string str;
    	scanf("%d%d\n",&n,&m);
    	while(v.push_back(gamer_()),cin>>v[++player].name)
    	{
    		for(int prb=1,j,jj,temp;prb<=n;prb++)
    		{
    			cin>>str;
    			if(str[0]<='0'||str[0]>'9')
    				continue;
    			v[player].accept++;
    			for(temp=j=0,jj=str.size();str[j]>='0'&&str[j]<='9'&&j<jj;j++)
    				temp=(temp<<1)+(temp<<3)+str[j]-'0';
    			v[player].penalty+=temp;
    			// cerr<<str<<"\t"<<temp<<'\t';
    			temp=0;
    			if(str[j]=='(')
    				for(j++;str[j]^')';j++)
    					temp=(temp<<1)+(temp<<3)+str[j]-'0';
    			v[player].penalty+=temp*m;
    			// cerr<<'('<<temp<<')'<<endl;
    		}
    	}
    	v.pop_back();
    	sort(v.begin(),v.end());
    	for(int i=0,ii=v.size();i<ii;i++)
    		v[i].output();
    	return 0;
    }
    
  • 1.3.7.Saving HDU(P2111):

    • At first glance, I thought that multiple backpacks were actually greedy. In order, just choose the ones with high value liao~
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    int input()
    {
    	char ch;
    	while((ch=getchar())<'0'||ch>'9');
    	int ret=ch-'0';
    	while((ch=getchar())>='0'&&ch<='9')
    		ret=(ret<<1)+(ret<<3)+ch-'0';
    	return ret;
    }
    
    struct treasure_
    {
    	int p,m;
    	treasure_(){}
    	treasure_(int a,int b):
    		p(a),m(b){}
    	treasure_ in()
    	{
    		p=input();
    		m=input();
    		return treasure_(p,m);
    	}
    	void output()
    	{
    		cout<<"<treasure_> : "<<p<<'\t'<<m<<endl;
    		return;
    	}
    };
    
    bool operator<(treasure_ a,treasure_ b)
    {
    	if(a.p^b.p)
    		return a.p<b.p;
    	return a.m<b.m;
    }
    treasure_ operator+(treasure_ a,treasure_ b)
    {
    	return treasure_(a.p+b.p,a.m+b.m);
    }
    
    int main()
    {
    	int v,n,anss;
    	vector<treasure_>item;
    	while(scanf("%d",&v),v)
    	{
    		scanf("%d",&n);
    		item.clear();
    		anss=0;
    		for(int i=0;i<n;i++)
    			item.push_back(treasure_().in());
    		sort(item.begin(),item.end());
    		// for(auto it:item)
    		// 	it.output();
    		while(v&&!item.empty())
    		{
    			if(v>=item.back().m)
    			{
    				anss+=item.back().p*item.back().m;
    				v-=item.back().m;
    			}
    			else
    			{
    				anss+=item.back().p*v;
    				v=0;
    				break;
    			}
    			// item.back().output();
    			// cout<<"after this anss = "<<anss<<endl;
    			item.pop_back();
    		}
    		printf("%d\n",anss);
    	}
    	return 0;
    }
    
  • 1.3.8.As Easy As A+B(P1040):

    • It's a naked sorting problem
    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    int input()
    {
    	int ret;
    	scanf("%d",&ret);
    	return ret;
    }
    
    int main()
    {
    	int T,n;
    	vector<int>v;
    	scanf("%d",&T);
    	for(int cas=1;cas<=T;cas++)
    	{
    		v.clear();
    		scanf("%d",&n);
    		for(int i=0;i<n;i++)
    			v.push_back(input());
    		sort(v.begin(),v.end());
    		for(int i=0;i<n;i++)
    			printf("%d%s",v[i],n-i-1?" ":"\n");
    	}
    	return 0;
    }
    

Chapter Two - phase 2

Section One - preliminary number theory

**Preliminary number theory: * * maximum common divisor (GCD), minimum common multiple (LCM), sieve prime

2.1.1. Least common multiple (P1108,LCM)

  • LCM (Least common multiple) and GCD (Greatest common divisor) have the following relationships:

    L C M ( a , b ) = a ∗ b / G C D ( a , b ) LCM(a,b)=a*b/GCD(a,b) LCM(a,b)=a∗b/GCD(a,b)

  • GCD can be obtained by rolling phase division

#include<iostream>

using namespace std;


int gcd(int a,int b)
{
	while(a)
	{
		b^=a;
		a^=b;
		b^=a;
		a%=b;
	}
	return b;
}

int main()
{
	int a,b;
	while(~scanf("%d%d",&a,&b))
	{
		printf("%d\n",a*b/gcd(a,b));
	}
	return 0;
}

2.1.2.How many prime numbers(P2138)

  • Sieve number and judgment
#include<iostream>
#include<vector>
#include<bitset>

using namespace std;

#define SIZE_ 46441//ceil(sqrt(2147483648))+100
vector<unsigned int>prime;
bitset<SIZE_>isnp;
void built_prime()
{
	for(int i=2;i<SIZE_;i++)
	{
		if(!isnp[i])
			prime.push_back(i);
		for(auto it:prime)
		{
			if(it*i>=SIZE_)
				break;
			isnp[it*i]=true;
		}
	}
	// for(auto it:prime)
	// 	cout<<it<<endl;
	return;
}
bool isp(int a)//Is A a prime?
{
	//If it is judged within the traversal range, it will be divided by itself in the following loop
	if(a<SIZE_)
		return !isnp[a];
	for(auto it:prime)
		if(a%it==0)
			return false;
	return true;
}

int main()
{
	built_prime();
	int n,v,anss;
	while(~scanf("%d",&n))
	{
		anss=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&v);
			anss+=isp(v);
		}
		printf("%d\n",anss);
	}
	return 0;
}

2.1.3. Encounter period (P1713)

  • Pursuit problem and score processing
  • Analyze sample data:
Sample Input:
2
26501/6335 18468/42
29359/11479 15725/19170
[v1 v2]

Sample Output:
81570078/7
5431415
[anss]

# Sample_1 :
anss % v1 = 0
anss % v2 = 0
anss / v1 = 2785590
anss / v2 = 26501
gcd of above2 : 1

# Sample_2 :
anss % v1 = 0
anss % v2 = 0
anss / v1 = 2123615
anss / v2 = 6621318
gcd of above2 : 1

# therefore :
anss=lcm(v1,v2)//At the score level

2.1.4.

2.1.5.

2.1.6.

2.1.7.Leftmost Digit(P1060)

  • seek n n n^n Highest digit of nn

  • Try to hard calculate the exponential result, but even the fast power needs to be calculated ( 1 0 5 ) ( 1 0 5 ) {(10^5)}^{(10^5)} 4.1s for both (105) and (105)

  • But when you see the index, you should think of its inverse operation to calculate the logarithm. Combined with the scientific counting method, there are:

∵ section learn meter number method have n n = x . . . . . × 1 0 y ∴ l o g 10 n n = n × l o g 10 n = l o g 10 x . . . . . + y ∴ l o g 10 x . . . . . = n × l o g 10 n − y ∵ his in 1 ≤ x . . . . . < 10 ∴ l o g 10 x . . . . . < 1 , y = f l o o r ( n × l o g 10 n ) ∴ x = 1 0 l o g 10 x = 1 0 n × l o g 10 n − f l o o r ( n × l o g 10 n ) \Because the scientific counting method has n ^ n = X_ {....} \times10^y\ \therefore log_ {10}{n^n}=n\times log_ {10}n=log_ {10}x._ {....}+ y\ \therefore log_ {10}x._ {....}= n\times log_ {10} N-y \ \ \ because where 1 \ Le X_ {....}< 10\ \therefore log_ {10}x._ {....}< 1,y=floor(n\times log_{10}n)\ \therefore x=10^{log_{10}x}=10^{n\times log_{10}n-floor(n\times log_{10}n)} ∵ the scientific counting method has nn=x ​ × 10y∴log10​nn=n × log10​n=log10​x..... ​+y∴log10​x..... ​=n × Log10 n − y ∵ where 1 ≤ x ​<10∴log10​x..... ​<1,y=floor(n × log10​n)∴x=10log10​x=10n × log10​n−floor(n × log10​n)

  • To sum up, we can finally get the key to solving the problem:

    double temp=n*log(n)/log(10);

    double anss=pow(10,temp-floor(temp));

    printf("%.0lf\n",floor(anss));

  • Afterwards: the formula of the conclusion is very similar to the formula of Euler's power reduction

#include<bits/stdc++.h>

using namespace std;


int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		double temp=1.*n*log(n)/log(10);
		int anss=floor(pow(10,temp-floor(temp)));
		printf("%d\n",anss);
	}
	return 0;
}
  • Similarly, it can be expressed by scientific counting n m n^m nm (multiple groups of data, each group occupies one line, including two positive integers n and m)
#include<bits/stdc++.h>

using namespace std;



int main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		double temp=1.*m*log(n)/log(10);
		double x=pow(10,temp-floor(temp));
		int y=floor(temp);
		printf("%lf x 10^%d\n",x,y);
	}
	return 0;
}

2.1.8. Decimal fraction 2(P1717)

  • At this time, a more comprehensive basic mathematical problem requires us to ( 0 , 1 ) (0,1) The rational decimal between (0,1) is converted to fractional form (eg.0.086(0123))
  • Fractional form of finite digits: reduced significant digits / 10 ^ significant digits (eg 123 9999 = 41 3333 \frac{123}{9999}=\frac{41}{3333} 9999123​=333341​)
  • Fractional form of cyclic digits: simplified significant digits / significant digits 9 (eg 86 1000 = 43 500 \frac{86}{1000}=\frac{43}{500} 100086​=50043​)
  • Pay attention to the leading 0 in the last two steps!!
  • Find the fractional form of finite digits and the fractional form sum of the cyclic part (eg 41 3333 + 43 500 = a n s s \frac{41}{3333}+\frac{43}{500}=anss 333341​+50043​=anss)
  • The middle may exceed int, so the data should be of long type
#include<iostream>
#include<string>

using namespace std;

//Change int except main() to TNT, and then.. [last step of WA to AC]
#define TNT long long

//Greatest common divisor (GCD)
TNT gcd(TNT a,TNT b)
{
	while(a)
	{
		a^=b;
		b^=a;
		a^=b;
		a%=b;
	}
	return b;
}

//Least common multiple (LCM)
TNT lcm(TNT a,TNT b)
{
	return a/gcd(a,b)*b;
}

//Reduction of a fraction
pair<TNT,TNT>red(pair<TNT,TNT>a)
{
	TNT temp=gcd(a.first,a.second);
	return pair<TNT,TNT>(a.first/temp,a.second/temp);
}

//the maximum number of the same digits (99.. 9)
TNT sd9(TNT a)//same digits of 9s
{
	TNT ret=0;
	while(a)
	{
		ret=(ret<<1)+(ret<<3)+9;
		a/=10;
	}
	return ret;
}

//10^a
TNT pow10(TNT a)
{
	TNT ret=1;
	while(a--)
		ret=(ret<<1)+(ret<<3);
	return ret;
}

//Fractional addition
pair<TNT,TNT>frac_plus(pair<TNT,TNT>a,pair<TNT,TNT>b)
{
	TNT temp=lcm(a.second,b.second);
	return red(pair<TNT,TNT>(a.first*temp/a.second+b.first*temp/b.second,temp));
}


int main()
{
	TNT T,pre_a,pre_b,a,b,i;//a: Limited parts; b: Circulation part; It does not exist when it is equal to - 1(EOF)
	string str;
	pair<TNT,TNT>pa,pb;//a,b in fractional form
	cin>>T;
	while(T--)
	{
		cin>>str;
		b=-1;
		pre_a=pre_b=a=0;
		for(i=2;str[i]=='0'&&i<(TNT)str.size();i++)pre_a++;
		for(;str[i]>='0'&&str[i]<='9'&&i<(TNT)str.size();i++)
			a=(a<<1)+(a<<3)+str[i]-'0';
		a=a?a:-1;
		if(str[i]=='(')
		{
			for(i++;str[i]=='0';i++)pre_b++;
			for(b=0;str[i]>='0'&&str[i]<='9'&&i<(TNT)str.size();i++)
				b=(b<<1)+(b<<3)+str[i]-'0';
		}
		// cout<<str<<endl
		// 	<<"a = "<<a<<'('<<pre_a<<')'<<endl
		// 	<<"b = "<<b<<'('<<pre_b<<')'<<endl;
		if(~a)pa=red(pair<TNT,TNT>(a,(sd9(a)+1)*pow10(pre_a)));
		else pa=pair<TNT,TNT>(0,1);
		if(~b)pb=red(pair<TNT,TNT>(b,sd9(b*pow10(pre_b))*pow10(pre_a)*(~a?(sd9(a)+1):1)));
		else pb=pair<TNT,TNT>(0,1);
		
		// Fractional form anss = fractional form pa + fractional form pb;
		pa=red(frac_plus(pa,pb));
		cout<<pa.first<<'/'<<pa.second<<endl;
	}
	return 0;
}

Problems Set

P1000 A + B Problem

  • This is the most basic indefinite group data input and output exercise
#include<iostream>

using namespace std;



int main()
{
	int a,b;
	while(~scanf("%d%d",&a,&b))
		printf("%d\n",a+b);
	return 0;
}

P1001 Sum Problem

  • This is a mathematical summation formula problem
  • followed by a blank line
#include<iostream>

using namespace std;



int main()
{
	unsigned int n;
	while(~scanf("%d",&n))
	{
		printf("%u\n\n",n*(n+1)>>1);
	}
	return 0;
}

P1002 A + B Problem II

  • This is a highly refined template question
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n;
	string a,b;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		if(i^1)putchar('\n');
		cin>>a>>b;
		printf("Case %d:\n%s + %s = %s\n",i,a.c_str(),b.c_str(),add(a,b).c_str());
	}
	return 0;
}

P1003 Max Sum

  • Dynamic gauge dp, the sequence with the largest sum
  • There it is! Output a blank line between two cases!!
#include<iostream>

using namespace std;

int val[100007];

int main(void)
{
	int T,n,sum,l,r,val_min,nex_l;
	scanf("%d",&T);
	for(int cas=1;cas<=T;cas++)
	{
		if(cas^1)putchar('\n');
		
		scanf("%d",&n);
		
		scanf("%d",val+1);
		l=r=nex_l=1;
		sum=val[1];
		val_min=0;
		if(val[1]<val_min)
		{
			nex_l=2;
			val_min=val[1];
		}
		for(int i=2;i<=n;i++)
		{
			scanf("%d",val+i);
			val[i]+=val[i-1];
			
			if(val[i]-val_min>sum)
			{
				l=nex_l;
				r=i;
				sum=val[i]-val_min;
			}
			if(val[i]<val_min)
			{
				nex_l=i+1;
				val_min=val[i];
			}
		}


		printf("Case %d:\n%d %d %d\n",cas,sum,l,r);

		// for (int i = 0; i <= n; ++i)
		// 	printf("%d ",val[i]);
		// printf("\n%d\n",val_min);
	}
	return 0;
}

P1004 Let the Balloon Rise

  • Use of basic container map

  • The classic sentence pattern appears:

    A test case with N = 0 terminates the input and this test case is not to be processed.

#include<iostream>
#include<string>
#include<map>

using namespace std;

map<string,int>counter;
map<string,int>::iterator it;

int main()
{
	int n,maxx;
	string str;
	while(scanf("%d",&n),n)
	{
		counter.clear();
		for(int i=1;i<=n;i++)
		{
			cin>>str;
			// cout<<str<<endl;
			counter[str]++;
		}
		maxx=0;
		for(it=counter.begin();it!=counter.end();it++)
		{
			if(it->second>maxx)
			{
				maxx=it->second;
				str=it->first;
			}
		}
		cout<<str<<endl;
	}
	return 0;
}

P1005 Number Sequence

  • Look at the data n ≤ 1 0 8 n\leq10^8 n ≤ 108, obviously this is a problem of finding rules
  • Then give the formula according to the question f ( 1 ) = 1 , f ( 2 ) = 1 , f ( n ) = ( A ∗ f ( n − 1 ) + B ∗ f ( n − 2 ) ) m o d 7 f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7 f(1)=1,f(2)=1,f(n)=(A∗f(n−1)+B∗f(n−2))mod7, f ( n ) f(n) f(n) only with f ( n − 1 ) f(n-1) f(n − 1) and f ( n − 2 ) f(n-2) f(n − 2) is related, and all three are only [ 0 , , 6 ] [0,,6] [0,, 6] these seven possible values
  • in short, f ( n − 1 ) ∈ [ 0 , , 6 ] , f ( n − 2 ) ∈ [ 0 , , 6 ] , f ( n ) f(n-1)\in[0,,6],f(n-2)\in[0,,6],f(n) The determinants of f(n − 1) ∈ [0, 6], f(n − 2) ∈ [0, 6], f(n) are only possible 7 × 7 = 49 7\times7=49 seven × 7 = 49 kinds, that is, at most one reincarnation for every 49 numbers, and every 49 numbers must be one reincarnation (verifiable Oh ~)
  • The first solution:
#include<iostream>

using namespace std;



int main()
{
	int a,b,n,val_2,val_1,val;
	while(scanf("%d%d%d",&a,&b,&n),a||b||n)
	{
		n%=49;
		val_2=val_1=val=1;
		for(int i=3;i<=n;i++)
		{
			val=(a*val_1+b*val_2)%7;
			val_2=val_1;
			val_1=val;
		}
		printf("%d\n",val);
	}
	return 0;
}
  • The second solution is to find out the loop section (Note: it may not return to the beginning 1 , 1 1,1 1,1, I don't know why it's OK to write like this (A)
#include<iostream>
#include<vector>

using namespace std;

vector<int>sta;

int main()
{
    int a,b,n,i;
    while(scanf("%d%d%d",&a,&b,&n),a||b||n)
    {
        sta.clear();
        sta.push_back(1);
        sta.push_back(1);
        sta.push_back(1);
        for(i=3;i<10000;i++)
        {
            sta.push_back((a*sta[i-1]+b*sta[i-2])%7);
            if(sta[i]==1&&sta[i-1]==1)
                break;
        }
        if(i>n)
        {
            printf("%d\n",sta[n]);
            continue;
        }
        // for(int i=1;i<(int)sta.size();i++)
        //     cout<<sta[i]<<' ';
        i-=2;
        sta[0]=sta[i];
        printf("%d\n",sta[n%i]);
    }
    return 0;
}

P1006 Tick and Tick

  • Key words: three pointer relationship on clock. To draw a picture, first fix an hour hand, then determine the activity range of the minute hand (outside D ° in both directions of clockwise and counterclockwise), and then place the second hand at the position where some minute hands may be happy..
  • Finally got...

P1007 Quoit Design

P1008

P1009 FatMouse' Trade

  • [1.3.1]
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct room_
{
	double j,f;
	room_(){}
	room_(int a,int b):
		j(a),f(b){}
	double rate()
	{return j/f;}
	room_ input()
	{
		scanf("%lf%lf",&j,&f);
		return *this;
	}
};
bool operator<(room_ a,room_ b)
{return a.rate()<b.rate();}


int main()
{
	double n,m,anss;
	vector<room_>v;
	while(scanf("%lf%lf",&m,&n),n!=-1||m!=-1)
	{
		v.clear();
		anss=0;
		for(int i=0;i<n;i++)
			v.push_back(room_().input());
		sort(v.begin(),v.end());
		// for(int i=0;i<n;i++)
		// 	cout<<v[i].j<<'\t'<<v[i].f<<endl;
		while(m&&!v.empty())
		{
			// cout<<v.back().j<<'\t'<<v.back().f<<endl;
			if(m>=v.back().f)
			{
				anss+=v.back().j;
				m-=v.back().f;
			}
			else
			{
				anss+=v.back().j*m/v.back().f;
				break;
			}
			v.pop_back();
		}
		printf("%.3lf\n",anss);
	}
	return 0;
}

P1010

P1011

P1012 u Calculate e

  • Simple output questions, from the example, pay attention to the number of digits after the decimal point (from 3 to the ninth)
#include<iostream>

using namespace std;



int main()
{
	double anss=1;
	unsigned long long factorial=1;
	printf("n e\n- -----------\n0 1\n");
	for(int i=1;i<=9;i++)
	{
		factorial*=i;
		anss+=1./factorial;
		printf("%d ",i);
		if(i<=2)
			cout<<anss<<endl;
		else
			printf("%.9lf\n",anss);
	}
	return 0;
}

P1013

P1014

P1015

P1016

P1017

P1018

P1019

P1020 Encoding

  • Simple string compression
#include<iostream>
#include<string>

using namespace std;



int main()
{
	int T,i,ii,times;
	string str;

	scanf("%d",&T);
	while(T--)
	{
		cin>>str;
		for(i=0,ii=str.size()-1;i<ii;i++)
		{
			if(str[i]==str[i+1])
			{
				times=2;
				i++;
				while(str[i]==str[i+1])
				{
					times++;
					i++;
				}
				printf("%d%c",times,str[i-1]);
			}
			else
			{
				putchar(str[i]);
			}
		}
		putchar('\n');
	}

	return 0;
}

P1021

P1022

P1023

P1024

P1025

P1026

P1027

P1028

P1029

P1030

P1031

P1032

  • The general question of the horn Valley conjecture (hail conjecture): ask the longest length of the horn Valley sequence (i would like to call it this) between i and j
  • Note that i is not necessarily equal to j
#include<bits/stdc++.h>

using namespace std;


int main()
{
	int i,j,anss,cnt,temp;
	while(~scanf("%d%d",&i,&j))
	{
		printf("%d %d ",i,j);
		if(i>j)
			swap(i,j);
		anss=0;
		while(i<=j)
		{
			for(cnt=1,temp=i;temp^1;cnt++)
				temp=(temp&1)?(temp*3+1):(temp>>1);
			anss=max(anss,cnt);
			i++;
		}
		printf("%d\n",anss);
	}
	return 0;
}

P1033

P1034

P1035 Robot Motion

  • If you follow the rules, there may be a ring or walk out of the maze, and the processing result can be obtained
#include<bits/stdc++.h>


using namespace std;


#define pii pair<int,int>

#define move_N pii(-1, 0)//^
#define move_S pii( 1, 0)//v
#define move_E pii( 0, 1)//>
#define move_W pii( 0,-1)//<
pii char_to_pii(char ch)
{
	switch(ch)
	{
		case 'N':
			return move_N;
		case 'S':
			return move_S;
		case 'E':
			return move_E;
		case 'W':
			return move_W;
	}
	return pii(0,0);
}

int now_n,now_m;

bool now_inside(pii loc)
{
	if(loc.first<1||loc.first>now_n||loc.second<1||loc.second>now_m)
		return false;
	return true;
}

void operator+=(pii&a,pii b)
{
	a.first+=b.first;
	a.second+=b.second;
	return;
}

int main()
{
	int now_start,now_step;
	vector<string>now_mapp;
	map<pii,int>now_route;
	pii now_loc,anss;
	string str;


// //test:(pii)+=(pii)
// now_loc=pii(10,10);
// now_loc+=pii(1,-1);
// cout<<now_loc.first<<endl<<now_loc.second<<endl;
// cin>>str;


	while(scanf("%d%d",&now_n,&now_m),now_n||now_m)
	{
		scanf("%d\n",&now_start);
		now_step=0;
		now_mapp.clear();
		now_mapp.push_back("");
		now_loc=pii(1,now_start);
		now_route.clear();
		anss=pii(0,0);

		for(int i=1;i<=now_n;i++)
		{
			cin>>str;
			now_mapp.push_back(" "+str);
		}


		while(now_inside(now_loc))
		{
			now_step++;

			if(now_route[now_loc])//cycle appearance
			{
				anss=pii(now_route[now_loc]-1,now_step-now_route[now_loc]);
				break;
			}

			now_route[now_loc]=now_step;
			now_loc+=char_to_pii(now_mapp[now_loc.first][now_loc.second]);
		}
		if(anss==pii(0,0))
			anss=pii(now_step,0);


		if(anss.second)//cycle
		{
			printf("%d step(s) before a loop of %d step(s)\n", anss.first,anss.second);
		}
		else//exit
		{
			printf("%d step(s) to exit\n",anss.first);
		}


		// for(int i=1;i<=now_n;i++,putchar('\n'))
		// 	for(int j=1;j<=now_m;j++)
		// 		putchar(now_mapp[i][j]);
		// putchar('\n');
		// for(int i=1;i<=now_n;i++,putchar('\n'))
		// 	for(int j=1;j<=now_m;j++)
		// 		printf("%4d",now_route[pii(i,j)]);
		// cout<<endl;
		// cout<<"now_loc : "<<now_loc.first<<"\t"<<now_loc.second<<endl;
		// cout<<"anss    : "<<anss.first<<"\t"<<anss.second<<endl;
		// cout<<endl<<endl;
	}


	return 0;
}

P1036

P1037

P1038

P1039

P1040 As Easy As A+B

  • 1.3.8
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int input()
{
	int ret;
	scanf("%d",&ret);
	return ret;
}

int main()
{
	int T,n;
	vector<int>v;
	scanf("%d",&T);
	for(int cas=1;cas<=T;cas++)
	{
		v.clear();
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			v.push_back(input());
		sort(v.begin(),v.end());
		for(int i=0;i<n;i++)
			printf("%d%s",v[i],n-i-1?" ":"\n");
	}
	return 0;
}

P1041

P1042~1048

P1049 Climbing Worm

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	int n,u,d;
	while(scanf("%d%d%d",&n,&u,&d),n|u|d)
	{
		printf("%d\n",((n-u)/(u-d)+bool((n-u)%(u-d)))*2+1);
	}
	return 0;
}

P1050~1059

P1060 Leftmost Digit

  • [2.1.7]
#include<bits/stdc++.h>

using namespace std;



int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		double temp=1.*n*log(n)/log(10);
		int anss=floor(pow(10,temp-floor(temp)));
		printf("%d\n",anss);
	}
	return 0;
}
  • Similarly, it can be expressed by scientific counting n m n^m nm (multiple groups of data, each group occupies one line, including two positive integers n and m)
#include<bits/stdc++.h>

using namespace std;



int main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		double temp=1.*m*log(n)/log(10);
		double x=pow(10,temp-floor(temp));
		int y=floor(temp);
		printf("%lf x 10^%d\n",x,y);
	}
	return 0;
}

P1061 Rightmost Digit

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		switch(n%10)
		{
			case 0:puts(n?"0":"1");break;
			case 1:puts("1");break;
			case 2:
				switch(n%4)
				{
					case 2:puts("4");break;
					case 0:puts("6");break;
				}
				break;
			case 3:
				switch(n%4)
				{
					case 1:puts("3");break;
					case 2:puts("9");break;
					case 3:puts("7");break;
					case 0:puts("1");break;
				}
				break;
			case 4:puts("6");break;
			case 5:puts("5");break;
			case 6:puts("6");break;
			case 7:
				switch(n%4)
				{
					case 1:puts("7");break;
					case 2:puts("9");break;
					case 3:puts("3");break;
					case 0:puts("1");break;
				}
				break;
			case 8:
				switch(n%4)
				{
					case 2:puts("4");break;
					case 0:puts("6");break;
				}
				break;
			case 9:puts("9");break;
		}
	}
	return 0;
}

P1062~1088

P1089 A+B for Input-Output Practice (I)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	string a,b;
	while(cin>>a>>b)
	{
		printf("%s\n",add(a,b).c_str());
	}
	return 0;
}

P1090 A+B for Input-Output Practice (II)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n;
	string a,b;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		cin>>a>>b;
		printf("%s\n",add(a,b).c_str());
	}
	return 0;
}

P1091 A+B for Input-Output Practice (III)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	string a,b;
	while(cin>>a>>b,a!="0"||b!="0")
	{
		printf("%s\n",add(a,b).c_str());
	}
	return 0;
}

P1092 A+B for Input-Output Practice (IV)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n;
	string a,b;
	while(scanf("%d",&n),n)
	{
		cin>>a;
		for(int i=1;i<n;i++)
		{
			cin>>b;
			a=add(a,b);
		}
		printf("%s\n",a.c_str());
	}
	return 0;
}

P1093 A+B for Input-Output Practice (V)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n,T;
	string a,b;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		cin>>a;
		for(int i=1;i<n;i++)
		{
			cin>>b;
			a=add(a,b);
		}
		printf("%s\n",a.c_str());
	}
	return 0;
}

P1094 A+B for Input-Output Practice (VI)

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n;
	string a,b;
	while(~scanf("%d",&n))
	{
		cin>>a;
		for(int i=1;i<n;i++)
		{
			cin>>b;
			a=add(a,b);
		}
		printf("%s\n",a.c_str());
	}
	return 0;
}

P1095

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	string a,b;
	while(cin>>a>>b)
	{
		printf("%s\n\n",add(a,b).c_str());
	}
	return 0;
}

P1096

  • See the summary of [A-C1-S1] above for details
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string add(string a,string b)
{
	if(a.size()<b.size())swap(a,b);
	reverse(a.begin(),a.end());
	reverse(b.begin(),b.end());
	int i,ii=b.size();
	for(i=0;i<ii;i++)
	{
		a[i]+=b[i]-'0';
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	for(ii=a.size();i<ii;i++)
	{
		if(a[i]>'9')
		{
			a[i]-=10;
			a[i+1]++;
		}
	}
	if(a[ii]==1)
		a+='1';
	reverse(a.begin(),a.end());
	return a;
}

int main()
{
	int n,T;
	string a,b;
	scanf("%d",&T);
	// while(T--) 						// Method 1
	for(int cas=1;cas<=T;cas++)					//Method 2
	{
		if(cas^1)putchar('\n');					//Method 2
		scanf("%d",&n);
		cin>>a;
		for(int i=1;i<n;i++)
		{
			cin>>b;
			a=add(a,b);
		}
		printf("%s\n",a.c_str());
		// if(T)putchar('\n'); 			// Method 1
	}
	return 0;
}

P1097

P1098

P1099

P1106 sorting

  • See the summary of [A-C1-S3] above for details
#include<iostream>
#include<queue>


using namespace std;

char ch;//ch the next character that has not been processed
int tin_res=0;
char input_5()//Enter a valid number
{
	while(ch<'0'||ch>'9'||ch=='5')
		ch=getchar();
	tin_res=ch-'0';
	while((ch=getchar())>='0'&&ch<='9'&&ch!='5')
		tin_res=(tin_res<<1)+(tin_res<<3)+ch-'0';
	return ch;
}


#define tin_number 	 one 	// Input successful
#define tin_endl 	 two 	// End of line
#define tin_endflie 	 three 	// End of file
int try_input()//Try to enter and return the input result
{
	while(ch=='5')
		ch=getchar();
	if(ch=='\n')
		{
			ch=getchar();
			return tin_endl;
		}
	if(ch==EOF)
		return tin_endflie;
	input_5();
	return tin_number;
}

priority_queue<int,vector<int>,greater<int>>anss;

void output()//Output and clear the current priority queue (number of stored)
{
	while(!anss.empty())
	{
		printf("%d",anss.top());
		anss.pop();
		if(!anss.empty())
			putchar(' ');
	}
	return;
}

int main()
{
	ch=getchar();
	while(1)
	{
		switch(try_input())
		{
			case tin_number:
				// cout<<tin_res<<endl;
				anss.push(tin_res);
				break;
			case tin_endl:
				// cout<<"[\\n]"<<endl;
				output();
				putchar('\n');
				break;
			case tin_endflie:
				// cout<<"[end]"<<endl;
				output();
				return 0;
		}
	}
	return 0;
}

P1108 least common multiple

  • 2.1.1.
#include<iostream>

using namespace std;


int gcd(int a,int b)
{
	while(a)
	{
		b^=a;
		a^=b;
		b^=a;
		a%=b;
	}
	return b;
}

int main()
{
	int a,b;
	while(~scanf("%d%d",&a,&b))
	{
		printf("%d\n",a*b/gcd(a,b));
	}
	return 0;
}

P1196 Lowest Bit

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	int n;
	while(scanf("%d",&n),n)
	{
		printf("%d\n",(~n+1)&n);
	}
	return 0;
}

P1234 door opener and door closer

  • See the summary of [A-C1-S3] above for details
#include<iostream>
#include<string>

using namespace std;

struct tim_
{
	string name;
	int h,m,s;
	tim_(){};
	tim_(int a,int b,int c):
	h(a),m(b),s(c){}
	operator >(tim_ a)
	{
		if(h^a.h)
			return h>a.h;
		if(m^a.m)
			return m>a.m;
		if(s^a.s)
			return s>a.s;
		return false;
	}
	operator <(tim_ a)
	{
		if(h^a.h)
			return h<a.h;
		if(m^a.m)
			return m<a.m;
		if(s^a.s)
			return s<a.s;
		return false;
	}
}ne,op,ed;

int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		op=tim_(25,0,0);
		ed=tim_(-1,0,0);
		while(n--)
		{
			cin>>ne.name;
			scanf("%d:%d:%d",&ne.h,&ne.m,&ne.s);
			if(ne<op)
				op=ne;
			scanf("%d:%d:%d",&ne.h,&ne.m,&ne.s);
			if(ne>ed)
				ed=ne;
		}
		cout<<op.name<<' '<<ed.name<<endl;
	}
	return 0;
}

P1328 IBM Minus One

  • See the summary of [A-C1-S2] above for details
#include<iostream>
#include<string>

using namespace std;



int main()
{
	int n;
	string str;
	scanf("%d\n",&n);
	for(int cas=1;cas<=n;cas++)
	{
		printf("String #%d\n",cas);
		cin>>str;
		for(auto it:str)
			putchar(it^'Z'?it+1:'A');
		putchar('\n');
		putchar('\n');
	}
	return 0;
}

Fractional p1712

  • [2.1.8]
#include<iostream>
#include<string>

using namespace std;

//Change int except main() to TNT
#define TNT long long

//Greatest common divisor (GCD)
TNT gcd(TNT a,TNT b)
{
	while(a)
	{
		a^=b;
		b^=a;
		a^=b;
		a%=b;
	}
	return b;
}

//Least common multiple (LCM)
TNT lcm(TNT a,TNT b)
{
	return a/gcd(a,b)*b;
}

//Reduction of a fraction
pair<TNT,TNT>red(pair<TNT,TNT>a)
{
	TNT temp=gcd(a.first,a.second);
	return pair<TNT,TNT>(a.first/temp,a.second/temp);
}

//the maximum number of the same digits (99.. 9)
TNT sd9(TNT a)//same digits of 9s
{
	TNT ret=0;
	while(a)
	{
		ret=(ret<<1)+(ret<<3)+9;
		a/=10;
	}
	return ret;
}

//10^a
TNT pow10(TNT a)
{
	TNT ret=1;
	while(a--)
		ret=(ret<<1)+(ret<<3);
	return ret;
}

//Fractional addition
pair<TNT,TNT>frac_plus(pair<TNT,TNT>a,pair<TNT,TNT>b)
{
	TNT temp=lcm(a.second,b.second);
	return red(pair<TNT,TNT>(a.first*temp/a.second+b.first*temp/b.second,temp));
}


int main()
{
	TNT T,pre_a,pre_b,a,b,i;//a: Limited parts; b: Circulation part; It does not exist when it is equal to - 1(EOF)
	string str;
	pair<TNT,TNT>pa,pb;//a,b in fractional form
	cin>>T;
	while(T--)
	{
		cin>>str;
		b=-1;
		pre_a=pre_b=a=0;
		for(i=2;str[i]=='0'&&i<(TNT)str.size();i++)pre_a++;
		for(;str[i]>='0'&&str[i]<='9'&&i<(TNT)str.size();i++)
			a=(a<<1)+(a<<3)+str[i]-'0';
		a=a?a:-1;
		if(str[i]=='(')
		{
			for(i++;str[i]=='0';i++)pre_b++;
			for(b=0;str[i]>='0'&&str[i]<='9'&&i<(TNT)str.size();i++)
				b=(b<<1)+(b<<3)+str[i]-'0';
		}
		// cout<<str<<endl
		// 	<<"a = "<<a<<'('<<pre_a<<')'<<endl
		// 	<<"b = "<<b<<'('<<pre_b<<')'<<endl;
		if(~a)pa=red(pair<TNT,TNT>(a,(sd9(a)+1)*pow10(pre_a)));
		else pa=pair<TNT,TNT>(0,1);
		if(~b)pb=red(pair<TNT,TNT>(b,sd9(b*pow10(pre_b))*pow10(pre_a)*(~a?(sd9(a)+1):1)));
		else pb=pair<TNT,TNT>(0,1);
		
		// Fractional form anss = fractional form pa + fractional form pb;
		pa=red(frac_plus(pa,pb));
		cout<<pa.first<<'/'<<pa.second<<endl;
	}
	return 0;
}

P2138 How many prime numbers

  • [2.1.2]
#include<iostream>
#include<vector>
#include<bitset>

using namespace std;

#define SIZE_ 46441//ceil(sqrt(2147483648))+100
vector<unsigned int>prime;
bitset<SIZE_>isnp;
void built_prime()
{
	for(int i=2;i<SIZE_;i++)
	{
		if(!isnp[i])
			prime.push_back(i);
		for(auto it:prime)
		{
			if(it*i>=SIZE_)
				break;
			isnp[it*i]=true;
		}
	}
	// for(auto it:prime)
	// 	cout<<it<<endl;
	return;
}
bool isp(int a)//Is A a prime?
{
	//If it is judged within the traversal range, it will be divided by itself in the following loop
	if(a<SIZE_)
		return !isnp[a];
	for(auto it:prime)
		if(a%it==0)
			return false;
	return true;
}

int main()
{
	built_prime();
	int n,v,anss;
	while(~scanf("%d",&n))
	{
		anss=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&v);
			anss+=isp(v);
		}
		printf("%d\n",anss);
	}
	return 0;
}

P2161 Primes

  • In essence, it is necessary to screen prime numbers, but pay attention to the problem setting of this question (1 and 2 are not prime numbers in this question; when the input is less than or equal to 0, it will be GG)
#include<bits/stdc++.h>

using namespace std;

#define DATA_MAX 16007

vector<int>prime_table;
bitset<DATA_MAX>prime_isnp;
void prime_built()
{
	for(int i=2;i<DATA_MAX;i++)
	{
		if(!prime_isnp[i])
			prime_table.push_back(i);
		for(auto it:prime_table)
			if(i*it<DATA_MAX)
				prime_isnp[i*it]=true;
	}
	prime_isnp[1]=true;
	return;
}


int main()
{
	prime_built();
	prime_isnp[2]=true;
	int n,cas=1;
	while(scanf("%d",&n),n>0)
		printf("%d: %s\n",cas++,prime_isnp[n]?"no":"yes");
	return 0;
}

P2317 Nasty Hacks

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	int n,r,e,c;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d%d",&r,&e,&c);
		c=r-e+c;//Indicates the advantage of not joining advertising
		if(c)
			if(c>0)
				puts("do not advertise");
			else
				puts("advertise");
		else
			puts("does not matter");
	}
	return 0;
}

P2093 test ranking

  • [1.3.6]
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

struct gamer_
{
	string name;
	int accept,penalty;
	gamer_():
		name(""),accept(0),penalty(0){}
	void output()
	{
		// cout<<name<<'\t'<<accept<<'\t'<<penalty<<endl;
		printf("%-10s %2d %4d\n",name.c_str(),accept,penalty);
		return;
	}
	operator < (gamer_ a)
	{
		if(accept^a.accept)
			return accept>a.accept;
		if(penalty^a.penalty)
			return penalty<a.penalty;
		return name<a.name;
	}
};

int main()
{
	int n,m,player=-1;
	vector<gamer_>v;
	string str;
	scanf("%d%d\n",&n,&m);
	while(v.push_back(gamer_()),cin>>v[++player].name)
	{
		for(int prb=1,j,jj,temp;prb<=n;prb++)
		{
			cin>>str;
			if(str[0]<='0'||str[0]>'9')
				continue;
			v[player].accept++;
			for(temp=j=0,jj=str.size();str[j]>='0'&&str[j]<='9'&&j<jj;j++)
				temp=(temp<<1)+(temp<<3)+str[j]-'0';
			v[player].penalty+=temp;
			// cerr<<str<<"\t"<<temp<<'\t';
			temp=0;
			if(str[j]=='(')
				for(j++;str[j]^')';j++)
					temp=(temp<<1)+(temp<<3)+str[j]-'0';
			v[player].penalty+=temp*m;
			// cerr<<'('<<temp<<')'<<endl;
		}
	}
	v.pop_back();
	sort(v.begin(),v.end());
	for(int i=0,ii=v.size();i<ii;i++)
		v[i].output();
	return 0;
}

P2095 find your present (2)

  • See the summary of [A-C1-S2] above for details
#include<iostream>
#include<map>

using namespace std;

int input()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')
		ret=(ret<<1)+(ret<<3)+ch-'0';
	return ret;
}

int main()
{
	int n;
	map<int,int>counter;
	map<int,int>::iterator counter_i;
	while((n=input()))
	{
		counter.clear();
		for(int i=1;i<=n;i++)
			counter[input()]++;
		for(counter_i=counter.begin();counter_i!=counter.end();counter_i++)
		{
			if(counter_i->second&1)
			{
				printf("%d\n",counter_i->first);
				break;
			}
		}
	}
	return 0;
}

P2111 Saving HDU

  • [1.3.7]
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int input()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')
		ret=(ret<<1)+(ret<<3)+ch-'0';
	return ret;
}

struct treasure_
{
	int p,m;
	treasure_(){}
	treasure_(int a,int b):
		p(a),m(b){}
	treasure_ in()
	{
		p=input();
		m=input();
		return treasure_(p,m);
	}
	void output()
	{
		cout<<"<treasure_> : "<<p<<'\t'<<m<<endl;
		return;
	}
};

bool operator<(treasure_ a,treasure_ b)
{
	if(a.p^b.p)
		return a.p<b.p;
	return a.m<b.m;
}
treasure_ operator+(treasure_ a,treasure_ b)
{
	return treasure_(a.p+b.p,a.m+b.m);
}

int main()
{
	int v,n,anss;
	vector<treasure_>item;
	while(scanf("%d",&v),v)
	{
		scanf("%d",&n);
		item.clear();
		anss=0;
		for(int i=0;i<n;i++)
			item.push_back(treasure_().in());
		sort(item.begin(),item.end());
		// for(auto it:item)
		// 	it.output();
		while(v&&!item.empty())
		{
			if(v>=item.back().m)
			{
				anss+=item.back().p*item.back().m;
				v-=item.back().m;
			}
			else
			{
				anss+=item.back().p*v;
				v=0;
				break;
			}
			// item.back().output();
			// cout<<"after this anss = "<<anss<<endl;
			item.pop_back();
		}
		printf("%d\n",anss);
	}
	return 0;
}

P2550 hundred steps wear poplar

  • See the summary of [A-C1-S3] above for details
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

int input()
{
	char ch;
	while((ch=getchar())<'0'||ch>'9');
	int ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')
		ret=(ret<<1)+(ret<<3)+ch-'0';
	return ret;
}

bool cmp(pair<int,int>a,pair<int,int>b)
{return a.first<b.first;}

void she(int a,int b)
{
	string str=">+";
	for(int i=a-2;i>0;i--)
		str+="-";
	str+="+>\n";
	while(b--)
		cout<<str;
	putchar('\n');
}

vector<pair<int,int>>v;

int main()
{
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		v.clear();
		for(int i=0,tem;i<n;i++)
			tem=input(),
			v.push_back(pair<int,int>(tem,input()));
		sort(v.begin(),v.end(),cmp);
		// for(int i=0;i<n;i++)		//Sorted Check
		// 	cout<<v[i].first<<' '<<v[i].second<<endl;
		for(int i=0;i<n;i++)
			she(v[i].first,v[i].second);
		// for(auto it:v)			//C++11
		// 	she(it.first,it.second);
	}
	return 0;
}

P2561 second small integer

  • See the summary of [A-C1-S3] above for details
#include<iostream>

using namespace std;



int main()
{
	int T,n,val,mi,mii;//The lowest decimal mi, the second decimal mii
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		mi=mii=0x7fffffff;
		while(n--)
		{
			scanf("%d",&val);
			if(val<=mi)
			{
				mii=mi;
				mi=val;
				continue;
			}
			if(val<mii)
			{
				mii=val;
			}
		}
		printf("%d\n",mii);
	}
	return 0;
}

P2719 The Seven Percent Solution

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	char ch;
	while((ch=getchar())^'#')
	{
		if(ch==' ')
		{
			putchar('%');
			putchar('2');
			putchar('0');
			continue;
		}
		if(ch=='!')
		{
			putchar('%');
			putchar('2');
			putchar('1');
			continue;
		}
		if(ch=='$')
		{
			putchar('%');
			putchar('2');
			putchar('4');
			continue;
		}
		if(ch=='%')
		{
			putchar('%');
			putchar('2');
			putchar('5');
			continue;
		}
		if(ch=='(')
		{
			putchar('%');
			putchar('2');
			putchar('8');
			continue;
		}
		if(ch==')')
		{
			putchar('%');
			putchar('2');
			putchar('9');
			continue;
		}
		if(ch=='*')
		{
			putchar('%');
			putchar('2');
			putchar('a');
			continue;
		}
		putchar(ch);
	}
	return 0;
}

P3188

  • See the summary of [A-C1-S2] above for details
#include<iostream>

using namespace std;



int main()
{
	int n,a,b,c;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d%d",&a,&b,&c);
		if(a>b)
			swap(a,b);
		if(b>c)
			swap(b,c);
		if(a>b)
			swap(a,b);
		if(a==b||b==c)
		{
			puts("perfect");
			continue;
		}
		if(a*a+b*b==c*c)
		{
			puts("good");
			continue;
		}
		puts("just a triangle");
	}
	return 0;
}

Accessories - Collection

High precision template (represented by vector_int, range positive integer)

/*
 * high-precision 	 Using vector_int represents a positive integer
 * 
 * 
 * Storage method:
 * BigNum_ val;
 * val[0]=Digital length;
 * val[1]=One digit number;
 * val[2]=Ten digits;
 * ......
 * val[val[0]]=Highest digit;
 * 
 * 
 * Completed:
 * 		transformation: 		 BigNum_trans 	 (val)
 * 		transformation: 		 BigNum_trans(val,ret)
 * 		transformation: 		 BigNum_trans 	 (str)
 * 		transformation: 		 BigNum_trans(str,ret)
 * 		transformation: 		 BigNum_trans(val,str)
 * 		Input:++ 	 BigNum_input 	 (val)
 * 		Output:-- 	 BigNum_output 	 (val)
 * 		Comparison: > =, < =, ><
 * 		Addition:+ 	 BigNum_addition 	 (a,b,ret)
 * 		Subtraction:- 	 BigNum_subtract 	 (a,b,ret)
 * 		Multiplication:* 	 BigNum_multiply 	 (a,b,ret)
 * 		Power:^ 	 BigNum_power 	 (a,b,ret)
 * 		Factorial: 		 BigNum_factorial(val,ret)
 * 		Digits: 		 BigNum_resize 	 (ret,val) / / decimal operation
 * 		Division:/ 	 BigNum_division 	 (a,b,ret)
 * 		Remainder:% 	 BigNum_modulo 	 (a,b,ret)
 * 		Approximately: 		 BigNum_gcd 		 (a,b)
 * 		Small times: 		 BigNum_lcm 		 (a,b)
 * 		Approximately: 		 BigNum_gcd 		 (a,b,ret)
 * 		Small times: 		 BigNum_lcm 		 (a,b,ret)
 * 
 * hang in the air:
 * 
 */


#include<bits/stdc++.h>

using namespace std;


#define BigNum_ vector<int>


const BigNum_ BigNum_0  {1, 0};
const BigNum_ BigNum_1  {1, 1};
const BigNum_ BigNum_ERR{1,-1};




BigNum_ BigNum_trans(int val)
{
	BigNum_ ret;
	for(ret.push_back(0);val;ret[0]++)
	{
		ret.push_back(val%10);
		val/=10;
	}
	return ret;
}

void BigNum_trans(int val,BigNum_&ret)
{
	ret.clear();
	for(ret.push_back(0);val;ret[0]++)
	{
		ret.push_back(val%10);
		val/=10;
	}
	return;
}

BigNum_ BigNum_trans(string str)
{
	BigNum_ val;
	for(auto it:str)
		val.push_back(it-'0');
	val.push_back(str.size());
	reverse(val.begin(),val.end());
	return val;
}

void BigNum_trans(string str,BigNum_&ret)
{
	ret.clear();
	for(auto it:str)
		ret.push_back(it-'0');
	ret.push_back(str.size());
	reverse(ret.begin(),ret.end());
	return;
}

void BigNum_trans(BigNum_ val,string&str)
{
	str.clear();
	for(int i=val[0];i;i--)
		str+=(val[i]+'0');
	return;
}

void BigNum_input(BigNum_&val)
{
	val.clear();
	string str;
	cin>>str;
	for(auto it:str)
		val.push_back(it-'0');
	val.push_back(str.size());
	reverse(val.begin(),val.end());
	return;
}

void operator++(BigNum_&val)
{
	val.clear();
	string str;
	cin>>str;
	for(auto it:str)
		val.push_back(it-'0');
	val.push_back(str.size());
	reverse(val.begin(),val.end());
	return;
}

void operator++(BigNum_&val,int)
{
	val.clear();
	string str;
	cin>>str;
	for(auto it:str)
		val.push_back(it-'0');
	val.push_back(str.size());
	reverse(val.begin(),val.end());
	return;
}

void BigNum_output(BigNum_ val)
{
	for(int i=val[0];i;i--)
		putchar(val[i]+'0');
	return;
}

void operator--(BigNum_ val)
{
	for(int i=val[0];i;i--)
		putchar(val[i]+'0');
	return;
}

void operator--(BigNum_ val,int)
{
	for(int i=val[0];i;i--)
		putchar(val[i]+'0');
	return;
}


bool operator>=(BigNum_&val_a,BigNum_&val_b)
{
	if(val_a[0]^val_b[0])
		return val_a[0]>val_b[0];
	for(int i=val_a[0];i;i--)
		if(val_a[i]^val_b[i])
			return val_a[i]>val_b[i];
	return true;
}

bool operator<=(BigNum_&val_a,BigNum_&val_b)
{
	if(val_a[0]^val_b[0])
		return val_a[0]<val_b[0];
	for(int i=val_a[0];i;i--)
		if(val_a[i]^val_b[i])
			return val_a[i]<val_b[i];
	return true;
}

bool operator>(BigNum_&val_a,BigNum_&val_b)
{
	if(val_a[0]^val_b[0])
		return val_a[0]>val_b[0];
	for(int i=val_a[0];i;i--)
		if(val_a[i]^val_b[i])
			return val_a[i]>val_b[i];
	return false;
}

bool operator<(BigNum_&val_a,BigNum_&val_b)
{
	if(val_a[0]^val_b[0])
		return val_a[0]<val_b[0];
	for(int i=val_a[0];i;i--)
		if(val_a[i]^val_b[i])
			return val_a[i]<val_b[i];
	return false;
}

void BigNum_addition(BigNum_&val_a,BigNum_&val_b,BigNum_&ret)
{
	ret.clear();
	ret.push_back(0);
	int i,ii=min(val_a[0],val_b[0]),this_dig,next_dig=0;
	for(i=1;i<=ii;i++)
	{
		this_dig=val_a[i]+val_b[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	for(ii=val_a[0];i<=ii;i++)
	{
		this_dig=val_a[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	for(ii=val_b[0];i<=ii;i++)
	{
		this_dig=val_b[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	if(next_dig)
	{
		ret.push_back(next_dig);
		i++;
	}
	ret[0]=i-1;
	return;
}

BigNum_ operator+(BigNum_ val_a,BigNum_ val_b)
{
	BigNum_ ret;
	ret.push_back(0);
	int i,ii=min(val_a[0],val_b[0]),this_dig,next_dig=0;
	for(i=1;i<=ii;i++)
	{
		this_dig=val_a[i]+val_b[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	for(ii=val_a[0];i<=ii;i++)
	{
		this_dig=val_a[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	for(ii=val_b[0];i<=ii;i++)
	{
		this_dig=val_b[i]+next_dig;
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	if(next_dig)
	{
		ret.push_back(next_dig);
		i++;
	}
	ret[0]=i-1;
	return ret;
}

void BigNum_subtract(BigNum_&val_a,BigNum_&val_b,BigNum_&ret)
{
	if(val_a<val_b)
	{
		BigNum_subtract(val_b,val_a,ret);
		return;
	}
	ret=val_a;
	for(int i=val_b[0],j;i;i--)
	{
		ret[i]-=val_b[i];
		if(ret[i]<0)
		{
			for(j=i+1;!ret[j];j++)
				ret[j]=9;
			ret[j]--;
			ret[i]+=10;
		}
	}
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return;
}

BigNum_ operator-(BigNum_ val_a,BigNum_ val_b)
{
	if(val_a<val_b)
		return val_b-val_a;
	BigNum_ ret;
	ret=val_a;
	for(int i=val_b[0],j;i;i--)
	{
		ret[i]-=val_b[i];
		if(ret[i]<0)
		{
			for(j=i+1;!ret[j];j++)
				ret[j]=9;
			ret[j]--;
			ret[i]+=10;
		}
	}
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return ret;
}

void BigNum_multiply(BigNum_&val_a,BigNum_&val_b,BigNum_&ret)
{
	ret.clear();
	ret.push_back(val_a[0]+val_b[0]);
	for(int i=1,ii=ret[0],this_dig,next_dig=0;i<=ii;i++)
	{
		this_dig=next_dig;
		for(int j=1;j<=i;j++)
			if(val_a[0]>=j&&val_b[0]>=i-j+1)
				this_dig+=val_a[j]*val_b[i-j+1];
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	ret[0]-=!ret[ret[0]];
	return;
}

BigNum_ operator*(BigNum_ val_a,BigNum_ val_b)
{
	BigNum_ ret;
	ret.push_back(val_a[0]+val_b[0]);
	for(int i=1,ii=ret[0],this_dig,next_dig=0;i<=ii;i++)
	{
		this_dig=next_dig;
		for(int j=1;j<=i;j++)
			if(val_a[0]>=j&&val_b[0]>=i-j+1)
				this_dig+=val_a[j]*val_b[i-j+1];
		next_dig=this_dig/10;
		this_dig%=10;
		ret.push_back(this_dig);
	}
	ret[0]-=!ret[ret[0]];
	return ret;
}

void BigNum_power(BigNum_&val_a,BigNum_&val_b,BigNum_&ret)
{
	if(val_b[0]==1)
	{
		if(val_b[1]==0)
		{
			ret=BigNum_1;
			return;
		}
		if(val_b[1]==1)
		{
			ret=val_a;
			return;
		}
	}
	if(val_a[0]==1)
		if(val_a[1]==1||!val_a[1])
		{
			ret=val_a;
			return;
		}
	BigNum_ half_b,double_a;
	int this_dig,next_dig=0;
	half_b=val_b;
	for(int i=half_b[0];i;i--)
	{
		this_dig=next_dig;
		next_dig=half_b[i]&1?5:0;
		half_b[i]=half_b[i]/2+this_dig;
	}
	while(!half_b[half_b[0]])
		half_b[0]--;
	double_a=val_a*val_a;
	if(val_b[1]&1)
	{
		// ret=(double_a^half_b)*val_a;
		BigNum_ temp;
		BigNum_power(double_a,half_b,temp);
		BigNum_multiply(temp,val_a,ret);
	}
	else
	{
		// ret=double_a^half_b;
		BigNum_power(double_a,half_b,ret);
	}
	return;
}

BigNum_ operator^(BigNum_ val_a,BigNum_ val_b)
{
	if(val_b[0]==1)
	{
		if(val_b[1]==0)
			return BigNum_1;
		if(val_b[1]==1)
			return val_a;
	}
	if(val_a[0]==1)
		if(val_a[1]==1||!val_a[1])
			return val_a;
	BigNum_ half_b,double_a;
	int this_dig,next_dig=0;
	half_b=val_b;
	for(int i=half_b[0];i;i--)
	{
		this_dig=next_dig;
		next_dig=half_b[i]&1?5:0;
		half_b[i]=half_b[i]/2+this_dig;
	}
	while(!half_b[half_b[0]])
		half_b[0]--;
	double_a=val_a*val_a;
	if(val_b[1]&1)
		return (double_a^half_b)*val_a;
	else
		return double_a^half_b;
}

void BigNum_factorial(BigNum_&val,BigNum_&ret)
{
	ret=BigNum_1;
	for(BigNum_ i={1,1};i<=val;i=i+BigNum_1)
		ret=ret*i;
	return;
}

void BigNum_resize(BigNum_&ret,int val)
{
	reverse(ret.begin(),ret.end());
	ret.pop_back();
	ret.resize(val);
	ret.push_back(val);
	reverse(ret.begin(),ret.end());
	return;
}

void BigNum_division(BigNum_ val_a,BigNum_&val_b,BigNum_&ret)
{
	if(val_a<val_b)
	{
		ret=BigNum_ {1,0};
		return;
	}
	if(val_b==BigNum_0)
	{
		ret=BigNum_ERR;
		return;
	}
	ret.clear();
	ret.push_back(val_a[0]-val_b[0]+1);
	BigNum_resize(ret,ret[0]);
	for(int i=val_a[0],ii=val_b[0];i>=ii;i--)
	{
		BigNum_resize(val_b,i);
		while(val_a>=val_b)
		{
			val_a=val_a-val_b;
			ret[i-ii+1]++;
		}
	}
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return;
}

BigNum_ operator/(BigNum_ val_a,BigNum_ val_b)
{
	if(val_a<val_b)
		return BigNum_ {1,0};
	if(val_b==BigNum_0)
		return BigNum_ERR;
	BigNum_ ret;
	ret.push_back(val_a[0]-val_b[0]+1);
	BigNum_resize(ret,ret[0]);
	for(int i=val_a[0],ii=val_b[0];i>=ii;i--)
	{
		BigNum_resize(val_b,i);
		while(val_a>=val_b)
		{
			val_a=val_a-val_b;
			ret[i-ii+1]++;
		}
	}
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return ret;
}

void BigNum_modulo(BigNum_ val_a,BigNum_&val_b,BigNum_&ret)
{
	if(val_a<val_b)
	{
		ret=val_a;
		return;
	}
	if(val_b==BigNum_0)
	{
		ret=BigNum_ERR;
		return;
	}
	BigNum_resize(ret,ret[0]);
	for(int i=val_a[0],ii=val_b[0];i>=ii;i--)
	{
		BigNum_resize(val_b,i);
		while(val_a>=val_b)
			val_a=val_a-val_b;
	}
	ret=val_a;
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return;
}

BigNum_ operator%(BigNum_ val_a,BigNum_ val_b)
{
	if(val_a<val_b)
		return val_a;
	if(val_b==BigNum_0)
		return BigNum_ERR;
	BigNum_ ret;
	for(int i=val_a[0],ii=val_b[0];i>=ii;i--)
	{
		BigNum_resize(val_b,i);
		while(val_a>=val_b)
			val_a=val_a-val_b;
	}
	ret=val_a;
	while(!ret[ret[0]]&&ret[0]>1)
		ret[0]--;
	ret.resize(ret[0]+1);
	return ret;
}

BigNum_ BigNum_gcd(BigNum_ val_a,BigNum_ val_b)
{
	while(val_a>BigNum_0)
	{
		swap(val_a,val_b);
		val_a=val_a%val_b;
	}
	return val_b;
}

void BigNum_gcd(BigNum_ val_a,BigNum_ val_b,BigNum_&ret)
{
	while(val_a>BigNum_0)
	{
		swap(val_a,val_b);
		val_a=val_a%val_b;
	}
	ret=val_b;
	return;
}

BigNum_ BigNum_lcm(BigNum_ val_a,BigNum_ val_b)
{
	return val_a*val_b/BigNum_gcd(val_a,val_b);
}

void BigNum_lcm(BigNum_&val_a,BigNum_&val_b,BigNum_&ret)
{
	ret=val_a*val_b/BigNum_gcd(val_a,val_b);
	return;
}

int main()
{

    
    
    
    
	// //test_used:
	// BigNum_ a,b,c;


	// (BigNum_trans(321))--;cout<<endl<<endl;
	//(BigNum_trans("321"))--;cout<<endl<<endl;

    
	// while(true)
	// {

	// 	a++;
	// 	b++;
		// BigNum_input(a);
		// BigNum_input(b);
		// BigNum_output(a);
		// BigNum_output(b);


		// cout<<"(a>b)="<<(a>b)<<endl;
		// cout<<"(a<b)="<<(a<b)<<endl;
		// cout<<"(a>=b)="<<(a>=b)<<endl;
		// cout<<"(a<=b)="<<(a<=b)<<endl;

		// a--;
		// if(a==b)
		// 	cout<<"=";
		// if(a<b)
		// 	cout<<"<";
		// if(a>b)
		// 	cout<<">";
		// b--;cout<<endl;

		// // c=a+b;
		// BigNum_addition(a,b,c);
		// a--;cout<<"+";b--;cout<<"=";c--;cout<<endl;
		
		// // c=a-b;
		// BigNum_subtract(a,b,c);
		// a--;cout<<"-";b--;cout<<"=";c--;cout<<endl;
		
		// // c=a*b;
		// BigNum_multiply(a,b,c);
		// a--;cout<<"x";b--;cout<<"=";c--;cout<<endl;
		
		// // c=a/b
		
		// // c=a^b;
		// BigNum_power(a,b,c);
		// a--;cout<<"^";b--;cout<<"=";c--;cout<<endl;

		// // c=a!;
		// BigNum_factorial(a,c);
		// a--;cout<<"!=";c--;cout<<endl;

		// // c=a/b;
		// BigNum_division(a,b,c);
		// a--;cout<<"/";b--;cout<<"=";c--;cout<<endl;

		// // c=a%b;
		// BigNum_modulo(a,b,c);
		// a--;cout<<"%";b--;cout<<"=";c--;cout<<endl;

		// c=BigNum_gcd(a,b);
		// BigNum_gcd(a,b,c);
		// cout<<"gcd(";a--;cout<<",";b--;cout<<")=";c--;cout<<endl;

		// // c=BigNum_lcm(a,b);
		// BigNum_lcm(a,b,c);
		// cout<<"lcm(";a--;cout<<",";b--;cout<<")=";c--;cout<<endl;

	// }

	return 0;
}

Keywords: Algorithm acm

Added by dico on Thu, 17 Feb 2022 21:11:01 +0200