19级算法训练赛第七场

19级算法训练赛第七场

传送门:https://vjudge.net/contest/362412#problem/J

A - 程序设计:合并数字

蒜头君得到了 n 个数,他想对这些数进行下面这样的操作,选出最左边的相邻的差的绝对值为 1 的两个数,只保留较小的数,删去较大的数,直到没有两个相邻的差的绝对值为 1 的数,问最多可以进行多少次这样的操作?
输入格式
输入第一行为一个整数 n(1 \leq n \leq 10^5)n(1≤n≤10 5),表示数字的总数
第二行为 n个整数 x_1,x_2,…,x_n(0 \leq x_i \leq 10^9)x 1 ,x 2,…,x n (0≤x i ≤10 9 ),表示这些数。
样例输入
4
1 2 0 1
样例输出
3

栈的运用

#include
using namespace std;
stack<int> s;
int main(){
	int n,m,cnt=0;
	cin>>n;
	while(n--){
		cin>>m;
		while(!s.empty()&&s.top()-m==1){
			s.pop();
			cnt++;
		}
		if(!s.empty()&&m-s.top()==1) cnt++;
		else s.push(m);
	} 
	cout<<cnt<<endl;
	return 0;
}

B - 程序设计:找质数

一天蒜头君猜想,是不是所有的偶数(除了 2),都可以用两个质数相加得到呢?于是聪明的蒜头君就找你来验证了。

输入格式
第一行输入一个整数 t 表示测试组数。

接下来 tt 行,每行一个整数 n。

输出格式
输出两个整数,因为答案可能有多个,所有要求输出的这两个整数是所有答案中字典序最小的。

数据范围
对于 30%30% 的数据 1 \le t \le 10^31≤t≤10 3 。

对于 60%60% 的数据 1 \le t \le 10^51≤t≤10 5 。

对于 100%100% 的数据 1 \le t \le 10^6, 4 \le n \le 10^61≤t≤10 6 ,4≤n≤10 6,n为偶数。

。。。坑
先用质数筛打个表,多次询问且数值大,要用scanf,printf才不会超时

#include
#define ull unsigned long long
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
bool flag[maxn];
int prime[maxn/10];
void getPrime(){//函数欧拉筛找质数
    memset(flag,0,sizeof(flag));
    int cnt=0;
    for(int i=2;i*i<=maxn;i++)
        if(!flag[i])
            for(int j=i*i;j<=maxn;j+=i)
                flag[j]=1;
    for(int i=2;i<=maxn;i++)
        if(!flag[i]) prime[cnt++]=i;
}
int main(){
    getPrime();
    int t,num;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&num);
        for(int i=0;prime[i]<=num/2;i++){
        	if(!flag[num-prime[i]]){
            	printf("%d %d\n",prime[i],num-prime[i]);
            	break;
			}	
		}     
    }
    return 0;
}

C - Red and Black

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#





#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.

11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13

深度搜索,水洼问题

#include
using namespace std;
char c[23][23];
int vis[23][23];
int d[][2]={{-1,0},{0,-1},{0,1},{1,0}};
int n,m,cnt;
void dfs(int x,int y){
	cnt++;
	vis[x][y]=1;
	for(int i=0;i<4;i++)
		if(x+d[i][0]>=0&&y+d[i][1]>=0&&x+d[i][0]<m&&y+d[i][1]<n&&c[x+d[i][0]][y+d[i][1]]!='#'&&vis[x+d[i][0]][y+d[i][1]]!=1){
			dfs(x+d[i][0],y+d[i][1]);
		}
	return ;
}
int main(){
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		memset(vis,0,sizeof(vis));
		int x0,y0;
		cnt=0;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cin>>c[i][j];
				if(c[i][j]=='@'){
					x0=i;y0=j;
				}
			}
		}
		dfs(x0,y0);
		cout<<cnt<<endl;
	}	
	return 0;
}

D - Game 23

Polycarp plays “Game 23”. Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves.

Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so.

It is easy to prove that any way to transform n to m contains the same number of moves (i.e. number of moves doesn’t depend on the way of transformation).

Input
The only line of the input contains two integers n and m (1≤n≤m≤5⋅108).

Output
Print the number of moves to transform n to m, or -1 if there is no solution.

Examples
Input
120 51840
Output
7
Input
42 42
Output
0
Input
48 72
Output
-1
Note
In the first example, the possible sequence of moves is: 120→240→720→1440→4320→12960→25920→51840. The are 7 steps in total.

In the second example, no moves are needed. Thus, the answer is 0.

In the third example, it is impossible to transform 48 to 72.

题目要求就是输入两个数,问你从第一个数变成第二个数需要几步,变化的方法是乘以2或者乘以3,如果可以,输出步数,不可以的话就输出“-1”
我觉得主要是思维?

#include
using namespace std;

int main(){
	int n,m,cnt=0;
	cin>>n>>m;
	if(m%n!=0) cout<<-1<<endl;
	else{
		int r=m/n;
		for(int i=2;i<=3;i++){
			while(r%i==0){
				cnt++;
				r/=i;	
			}
		}
		if(r>1) cout<<-1<<endl;
		else cout<<cnt<<endl;
	}
	return 0;
}

E - 棋盘问题

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input
2 1
#.
.#
4 4
…#
…#.
.#…
#…
-1 -1
Sample Output
2
1

变种八皇后

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int n,k,cnt;
int vis[10];
char c[10][10];
void dfs(int h,int x){//h代表第几行,x代表个数
	if(x==k){
		cnt++;
		return ;
	}
	if(h>n){
		return;
	}
	for(int i=1;i<=n;i++){
		if(vis[i]==0&&c[h][i]=='#'){
			vis[i]=1;//相当于i这一列被占了 
			dfs(h+1,x+1);
			vis[i]=0;
		}
	}
	dfs(h+1,x);
}
int main(){
	while(cin>>n>>k){
		if(n==-1&&k==-1) break;
		cnt=0;
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				cin>>c[i][j];
			}	
		}
		dfs(1,0);
		cout<<cnt<<endl;
	} 
	return 0;
}

F - Super Jumping! Jumping! Jumping!

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the maximum according to rules, and one line one case.
Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
Sample Output
4
10
3

求解最大递增子序列和,需要注意的是不用是连续的。

#include
#define ull unsigned long long
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
int a[1008],dp[1008];
int main(){
	int n;
	while(cin>>n){
		if(n==0) break;
		memset(a,0,sizeof(a));
		memset(dp,0,sizeof(dp));
		int m=-maxn;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++){
			dp[i]=a[i];
			for(int j=1;j<=i;j++){
				if(a[j]<a[i]) dp[i]=max(dp[i],dp[j]+a[i]);
			}
			if(dp[i]>m) m=dp[i];
		}
		cout<<m<<endl;
	}
	return 0;
}

G - Primes

Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes.
Input
Each input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000.
Output
The output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by “yes” or “no”.
Sample Input
1
2
3
4
5
17
0
Sample Output
1: no
2: no
3: yes
4: no
5: yes
6: yes

质数判断

#include
using namespace std;
bool pd(long long num){/*速度最快*/ 
    if(num==1||num==2) return 0;
    if(num==3) return 1;   
    if(num%6!=1&&num%6!=5) return 0 ;  
    int tmp=sqrt(num);
    for(int i=5;i<=tmp;i+=6){
    	if(num%i==0||num%(i+2)==0) return 0;
    }
    return 1 ;
}
int main(){
	int n,cnt=1;
	while(cin>>n&&n>0){   
		if(pd(n)) printf("%d: yes\n",cnt);
		else printf("%d: no\n",cnt);
		cnt++;
	}
	return 0;
}

H - WeirdSort

You are given an array a of length n.

You are also given a set of distinct positions p1,p2,…,pm, where 1≤pi

Your task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps.

For example, if a=[3,2,1] and p=[1,2], then we can first swap elements a[2] and a[3] (because position 2 is contained in the given set p). We get the array a=[3,1,2]. Then we swap a[1] and a[2] (position 1 is also contained in p). We get the array a=[1,3,2]. Finally, we swap a[2] and a[3] again and get the array a=[1,2,3], sorted in non-decreasing order.

You can see that if a=[4,1,2,3] and p=[3,2] then you cannot sort the array.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

Then t test cases follow. The first line of each test case contains two integers n and m (1≤m

Output
For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print “NO”.

Example
Input
6
3 2
3 2 1
1 2
4 2
4 1 2 3
3 2
5 1
1 2 3 4 5
1
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
5 2
2 1 2 3 3
1 4
Output
YES
NO
YES
YES
NO
YES

输入两个数n和m
第二行n个数,表示长度为n的数组a
第三行m个数,p1,p2,p3…pm,每个数表示长度为n的数组中a[pi]可以和a[pi+1]互换
只允许进行m中互换,问能否实现a数组的从小到大排序
有is_sorted()函数的运用(判断一个数组是否从小到大有序),真是太方便辣!

#include
#include
using namespace std;
int a[103],p[103];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=m;i++) cin>>p[i];
		for(int i=1;i<=n;i++)
   			for(int j=1;j<=m;j++)
    			if(a[p[j]]>a[p[j]+1]) swap(a[p[j]],a[p[j]+1]);
    	if(is_sorted(a+1,a+n+1)) puts("YES");
		else puts("NO"); 
	}
	return 0;
}

I - Integer Divisibility

If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

For example you have to find a multiple of 3 which contains only 1’s. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3’s then, the result is 6, because 333333 is divisible by 7.

Input
Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input
3

3 1

7 3

9901 1

Sample Output
Case 1: 3

Case 2: 6

Case 3: 12

翻译
如果整数不能被2或5整除,则十进制表示法中该数字的某个倍数只是一个数字的序列。现在您将获得数字和唯一允许的数字,您应该报告此数字的位数。
例如,您必须找到仅包含1的3的倍数。然后结果是3,因为111(3位)可被3整除。同样,如果你发现7的多数只包含3的那么,结果是6,因为333333可以被7整除

大数整除

#include
#define ull unsigned long long
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;

int main(){
	int t;
	cin>>t;
	for(int i=1;i<=t;i++){
		ll n,m,d=0,b=1,temp=0;
		cin>>n>>m;
		while(b){
			temp=(temp*10+m)%n;
			b=temp;
			d++;
		}
        printf("Case %lld: %lld\n",i,d);
	}
	return 0;
}

J - Pie

My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.
Input
One line with a positive integer: the number of test cases. Then for each test case:
—One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
—One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.
Output
For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).
Sample Input
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
Sample Output
25.1327
3.1416
50.2655

分到最大馅饼的体积是:所有馅饼体积之和/(F+1);(为什么不能是直接所有馅饼之和/(F+1)呢?因为每人只能分到一块,如果讲究平均分的话,一个人分到的就是拼凑起来的“最大的体积”,一个人甚至可能得到超过一块馅饼)最小即为0,然后我们对这两个最大最小值采用二分方法,求出中间符合最大的体积。那么怎么判断那个值就是最大的体积呢?让每个馅饼的体积/能分到最大的体积=能分到的人数,然后将每个馅饼能分到的人数相加,将该人数与F+1做判断比较//转自「聪明绝顶的你与即将秃头的我」
二分(还是有点不懂)

#include
#define ull unsigned long long
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
double v[10008];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,f;
		double right,left=0.0,mid;
		cin>>n>>f;
		f++;
		for(int i=1;i<=n;i++){
			int r;
			cin>>r;
			v[i]=pi*r*r;
			right+=v[i];
		}
		right/=f;
		while(right-left>1e-6){
			int sum=0;
			mid=(right+left)/2.0;
			for(int i=1;i<=n;i++) sum+=(int)(v[i]/mid);
			if(sum>=f) left=mid;
			else right=mid;
		}
		printf("%.4lf\n",mid);
	}
	return 0;
}

K - 水果

夏天来了好开心啊,呵呵,好多好多水果
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.
Input
第一行正整数N(0 每组测试数据的第一行是一个整数M(0 Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
Sample Input
1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
Sample Output
guangdong
|----pineapple(5)
|----sugarcane(1)
shandong
|----apple(3)

map的嵌套(套了个结构体)重要

#include
#define ull unsigned long long
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
struct fruit{//存放水果和数量 
	map<string,int>fruitma;
};
map<string,fruit> ma;
int main(){
	int t;
	cin>>t;
	while(t--){
		ma.clear();
		int n;
		cin>>n;
		//存放地名和水果 
		for(int i=1;i<=n;i++){
			string sg,dm;
			int cnt;
			cin>>sg>>dm>>cnt;
			ma[dm].fruitma[sg]+=cnt;
		}
		map<string,fruit>::iterator it;
		map<string,int>::iterator fruitmait;
		for(it=ma.begin();it!=ma.end();it++){
			cout<<it->first<<endl;
			for(fruitmait=it->second.fruitma.begin();fruitmait!=it->second.fruitma.end();fruitmait++){
				cout<<"   |----"<<fruitmait->first<<"("<<fruitmait->second<<")"<<endl;
			}
		}
		if(t!=0) cout<<endl;
	}
	return 0;
}

L - Dead Pixel

Screen resolution of Polycarp’s monitor is a×b pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (x,y) (0≤x

Polycarp wants to open a rectangular window of maximal size, which doesn’t contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.

Print the maximal area (in pixels) of a window that doesn’t contain the dead pixel inside itself.

Input
In the first line you are given an integer t (1≤t≤104) — the number of test cases in the test. In the next lines you are given descriptions of t test cases.

Each test case contains a single line which consists of 4 integers a,b,x and y (1≤a,b≤104; 0≤x2 (e.g. a=b=1 is impossible).

Output
Print t integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn’t contain the dead pixel.

Example
Input
6
8 8 0 0
1 10 0 3
17 31 10 4
2 1 0 0
5 10 3 9
10 10 4 8
Output
56
6
442
1
45
80

思维,一个坏的像素将一个屏幕分成了四个部分,算出最大的那部分。

#include
#define ull unsigned long long
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;

int main(){
	int t;
	cin>>t;
	while(t--){
		int a,b,x,y,s1,s2,s3,s4;
		cin>>a>>b>>x>>y;
		s1=a*y;
		s2=a*(b-y-1);
		s3=b*(a-x-1);
		s4=b*x;
		s1=max(s1,s2);
		s3=max(s3,s4);
		cout<<max(s1,s3)<<endl;
	}
	return 0;
}

M - Complete the Sequence

You probably know those quizzes in Sunday magazines: given the sequence 1, 2, 3, 4, 5, what is the next number? Sometimes it is very easy to answer, sometimes it could be pretty hard. Because these “sequence problems” are very popular, ACM wants to implement them into the “Free Time” section of their new WAP portal.
ACM programmers have noticed that some of the quizzes can be solved by describing the sequence by polynomials. For example, the sequence 1, 2, 3, 4, 5 can be easily understood as a trivial polynomial. The next number is 6. But even more complex sequences, like 1, 2, 4, 7, 11, can be described by a polynomial. In this case, 1/2.n^2-1/2.n+1 can be used. Note that even if the members of the sequence are integers, polynomial coefficients may be any real numbers.

Polynomial is an expression in the following form:

P(n) = aD.nD+aD-1.nD-1+…+a1.n+a0

. If aD <> 0, the number D is called a degree of the polynomial. Note that constant function P(n) = C can be considered as polynomial of degree 0, and the zero function P(n) = 0 is usually defined to have degree -1.
Input
There is a single positive integer T on the first line of input. It stands for the number of test cases to follow. Each test case consists of two lines. First line of each test case contains two integer numbers S and C separated by a single space, 1 <= S < 100, 1 <= C < 100, (S+C) <= 100. The first number, S, stands for the length of the given sequence, the second number, C is the amount of numbers you are to find to complete the sequence.

The second line of each test case contains S integer numbers X1, X2, … XS separated by a space. These numbers form the given sequence. The sequence can always be described by a polynomial P(n) such that for every i, Xi = P(i). Among these polynomials, we can find the polynomial Pmin with the lowest possible degree. This polynomial should be used for completing the sequence.
Output
For every test case, your program must print a single line containing C integer numbers, separated by a space. These numbers are the values completing the sequence according to the polynomial of the lowest possible degree. In other words, you are to print values Pmin(S+1), Pmin(S+2), … Pmin(S+C).

It is guaranteed that the results Pmin(S+i) will be non-negative and will fit into the standard integer type.
Sample Input
4
6 3
1 2 3 4 5 6
8 2
1 2 4 7 11 16 22 29
10 2
1 1 1 1 1 1 1 1 1 2
1 10
3
Sample Output
7 8 9
37 46
11 56
3 3 3 3 3 3 3 3 3 3

差分,还得多学哇
19级算法训练赛第七场_第1张图片

#include
#define ull unsigned long long
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
int a[105][105];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=0;i<n;i++){
			cin>>a[0][i];
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<n-i;j++){
				a[i][j]=a[i-1][j+1]-a[i-1][j];
				//cout<
			}
			//cout<
		}
		for(int i=1;i<=m;i++){
			a[n-1][i]=a[n-1][0];
			//cout<
		}
		//cout<
		for(int i=n-2;i>=0;i--){
			for(int j=0;j<m;j++){
				a[i][n-i+j]=a[i][n-i+j-1]+a[i+1][n-i+j-1];
				//cout<
			}
			//cout<
		}
		for(int i=0;i<m-1;i++) cout<<a[0][n+i]<<" ";
		cout<<a[0][n+m-1]<<endl;
	}
	return 0;
}

N - Harmonic Number

In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

In this problem, you are given n, you have to find Hn.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

Sample Input
12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Sample Output
Case 1: 1

Case 2: 1.5

Case 3: 1.8333333333

Case 4: 2.0833333333

Case 5: 2.2833333333

Case 6: 2.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

求f(n)=1/1+1/2+1/3+1/4…1/n (1 ≤ n ≤ 108).,精确到10^-8

欧拉常数,欧拉公式传送门:https://www.cnblogs.com/guxuanqing/p/9440597.html

#include
#define ull unsigned long long
#define pi acos(-1)
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
const double r=0.57721566490153286060651209;//欧拉常数
double a[10000];
int main(){
    a[1]=1;
    for(int i=2;i<10000;i++){//预先把小于10000的f(n)求出来
        a[i]=a[i-1]+1.0/i;
    }
    int n; 
	cin>>n;
    for(int i=1;i<=n;i++){
        int x;
		cin>>x;
        if(x<10000){//n<10000时,可直接得出结果
            printf("Case %d: %.10lf\n",i,a[x]);
        }
        else{//否则利用欧拉公式
            double a=log(x)+r+1.0/(2*x);
            printf("Case %d: %.10lf\n",i,a);
        }
    }
    return 0;
}

你可能感兴趣的:(19级训练)