2017 ccpc女生专场 1003 Coprime Sequence

前缀后缀gcd,其实自己中用的是种奇怪的方法A掉的,不过先把这个学上,自己的方法有时间再填。

题意

告诉你N个数,求删除一个数可以求得最大GCD。

N可能是100000。

思路

这道题其实很简单,但是想不到这点就很难。

简单的说就是先预处理,得到每个数字左边的GCD和右边的GCD.

  1. befor(i)代表前i个数字的GCD, 复杂度 O(n*log(n))
  2. after(i)代表i之后的数字的GCD. 复杂度 O(n*log(n))
  3. ans = max(after(2), befor(1)+after(3), ..., befor(n-2)+after(n), befor(n-1)) ;

Coprime Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 250    Accepted Submission(s): 145


Problem Description
Do you know what is called ``Coprime Sequence''? That is a sequence consists of  n positive integers, and the GCD (Greatest Common Divisor) of them is equal to 1.
``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.
 

 

Input
The first line of the input contains an integer  T(1T10), denoting the number of test cases.
In each test case, there is an integer n(3n100000) in the first line, denoting the number of integers in the sequence.
Then the following line consists of n integers a1,a2,...,an(1ai109), denoting the elements in the sequence.
 

 

Output
For each test case, print a single line containing a single integer, denoting the maximum GCD.
 

 

Sample Input
3 3 1 1 1 5 2 2 2 3 2 4 1 2 4 8
 

 

Sample Output
1 2 2
 
 1 #include<iostream>
 2 #include<stdio.h>
 3 
 4 #define ff 1000000007
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int t,n,k;
11     scanf("%d",&t);
12     while(t--)
13     {
14         scanf("%d%d",&n,&k);
15         long long ans=0,temp;
16         for(int i = 1; i <= n; i++)
17         {
18             temp=1;
19             for(int j = 1; j <= k; j++)
20             {
21                 temp=(temp*i)%ff;
22             }
23             ans=(ans+temp)%ff;
24         }
25         printf("%lld\n",ans%ff);
26     }
27     
28     return 0;
29 }

 

转载于:https://www.cnblogs.com/Xycdada/p/6828531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值