BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem
Time Limit: 1000/1000 MS
(Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
GTY is a GodBull who will get an Au in NOI . To have more time to learn algorithm knowledge, he never does his math homework. His math teacher is very unhappy for that, but she can't do anything because GTY can always get a good mark in math exams. One day, the math teacher asked GTY to answer a question. There are four numbers on the blackboard - a,b,c,d. The math teacher wants GTY to compare ab with cd. Because GTY never does his homework, he can't figure out this problem! If GTY can't answer this question correctly, he will have to do his homework. So help him!
Input
Multi test cases (about 5000). Every case contains four integers a,b,c,d(1≤a,b,c,d≤1000)separated by spaces. Please process to the end of file.
Output
For each case , if ab>cd , print '>'. if ab<cd , print '<'. if ab=cd , print '='.
Sample Input
2 1 1 2
2 4 4 2
10 10 9 11
Sample Output
>
=
<
GTY's math problem
Time Limit: 1000/1000 MS
(Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1636 Accepted Submission(s): 298
问题描述
众所周知,GTY是一位神犇,为了更好的虐场,他从来不写数学作业而是去屠题,他的数学老师非常不爽,但由于GTY每次考试都AK,她也不能说什么,有一天老师在黑板上写了四个数——a,b,c,d 然后让GTY比较ab和cd的大小,由于GTY不屑于虐这道题,就把这个问题交给你了。
输入描述
多组数据(约5000组),每组数据包含4个整数a,b,c,d(1≤a,b,c,d≤1000),用空格隔开
输出描述
对于每组数据,若ab>cd,输出”>”, 若ab<cd,输出”<”, 若ab=cd,输出”=”
输入样例
2 1 1 2
2 4 4 2
10 10 9 11
输出样例
>
=
<
思路 log(a^b)=b∗log(a) 注意精度!
#include<cstdio> #include<cmath> #include<iostream> using namespace std; #define emp (1e-8) int main() { double a,b,c,d; while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF) { if(fabs(b*log(a)-d*log(c))<=emp) printf("=\n"); else if(b*log(a)>d*log(c)) printf(">\n"); else if(b*log(a)<d*log(c)) printf("<\n"); } return 0; }
GTY's birthday gift
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
FFZ's birthday is coming. GTY wants to give a gift to ZZF. He asked his gay friends what he should give to ZZF. One of them said, 'Nothing is more interesting than a number multiset.' So GTY decided to make a multiset for ZZF. Multiset can contain elements with same values. Because GTY wants to finish the gift as soon as possible, he will use JURUO magic. It allows him to choose two numbers a and b(a,b∈S), and add a+b to the multiset. GTY can use the magic for k times, and he wants the sum of the multiset is maximum, because the larger the sum is, the happier FFZ will be. You need to help him calculate the maximum sum of the multiset.
Input
Multi test cases (about 3) . The first line contains two integers n and k (2≤n≤100000,1≤k≤1000000000). The second line contains n elements ai (1≤ai≤100000)separated by spaces , indicating the multiset S .
Output
For each case , print the maximum sum of the multiset (mod 10000007).
Sample Input
3 2
3 6 2
Sample Output
35
GTY's birthday gift
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 585 Accepted Submission(s): 103
问题描述
GTY的朋友ZZF的生日要来了,GTY问他的基友送什么礼物比较好,他的一个基友说送一个可重集吧!于是GTY找到了一个可重集S,GTY能使用神犇魔法k次,每次可以向可重集中加入一个数 a+b(a,b∈S),现在GTY想最大化可重集的和,这个工作就交给你了。
注:可重集是指可以包含多个相同元素的集合
输入描述
多组数据(约3组),每组数据的第一行有两个数n,k(2≤n≤100000,1≤k≤1000000000) 表示初始元素数量和可使用的魔法数,第二行包含n个数a(1≤ai≤100000)表示初始时可重集的元素
输出描述
对于每组数据,模10000007输出可重集可能的最大和。
输入样例
3 2
3 6 2
输出样例
35
这题是典型的矩阵快速幂:
这是公式:
#include<cstdlib> #include<cstring> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define LL __int64 #define mod 10000007 int N; struct matrix { LL m[3][3]; }; int a[100010]; matrix multiply(matrix x,matrix y) { matrix temp; memset(temp.m,0,sizeof(temp.m)); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(x.m[i][j]==0) continue; for(int k=0; k<3; k++) { if(y.m[j][k]==0) continue; temp.m[i][k]+=x.m[i][j]*y.m[j][k]%mod; temp.m[i][k]%=mod; } } } return temp; } matrix quickmod(matrix a,int n) { matrix res;//单位阵 memset(res.m,0,sizeof(res.m)); for(int i=0;i<3;i++) res.m[i][i]=1; while(n) { if(n&1) res=multiply(res,a); n>>=1; a=multiply(a,a); } /* for(int i=0; i<3; i++) { for(int j=0; j<3; j++) printf("%8d",res.m[i][j]); printf("\n"); } printf("\n");*/ return res; } int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { LL sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n); matrix ans; ans.m[0][0]=1;ans.m[0][1]=1;ans.m[0][2]=1; ans.m[1][0]=0;ans.m[1][1]=1;ans.m[1][2]=1; ans.m[2][0]=0;ans.m[2][1]=1;ans.m[2][2]=0; ans=quickmod(ans,k); printf("%d\n",(ans.m[0][0]*sum+ans.m[0][1]*a[n-1]+ans.m[0][2]*a[n-2])%mod); } return 0; }