1421 最大MOD值

1421 最大MOD值

基准时间限制:1 秒 空间限制:131072 KB

有一个a数组,里面有n个整数。现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj。

Input

单组测试数据。
第一行包含一个整数n,表示数组a的大小。(1 ≤ n ≤ 2*10^5)
第二行有n个用空格分开的整数ai (1 ≤ ai ≤ 10^6)。

Output

输出一个整数代表最大的mod值。

Input示例

3
3 4 5

Output示例

2思路:二分;找最大的mod,我们先去重,然后循环每个数的倍数,二分找小余倍数的最大的值,这个用筛法,然后更新最大的答案。复杂度n*(logn)^2;
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<stdlib.h>
 6 #include<queue>
 7 #include<iostream>
 8 int ans[300000];
 9 int bns[300000];
10 int ap[1000005];
11 using namespace std;
12 int main(void) {
13     int n;
14     scanf("%d",&n);
15     int i,j;
16     int maxx = 0;
17     for(i = 0; i < n; i++) {
18         scanf("%d",&ans[i]);
19         maxx = max(ans[i],maxx);
20     }
21     sort(ans,ans+n);
22     int cn = 0;
23     bns[cn++] = ans[0];
24     int t =bns[0];
25     for(i = 1; i < n; i++) {
26         if(ans[i]!=t) {
27             t = ans[i];
28             bns[cn++] = t;
29         }
30     }
31     if(n == 1) {
32         printf("0\n");
33     } else {
34         int ask = 0;
35         for(i = cn-1; i >= 0; i--) {
36             if(bns[i]!=1) {
37                 for(j = 2; bns[i]*j <= maxx+bns[i]; j++) {
38                     int l = i+1,r = cn-1;
39                     int id = -1;
40                     while(l <= r) {
41                         int mid = (l+r)/2;
42                         if(bns[mid] < bns[i]*j) {
43                             id = mid;
44                             l = mid + 1;
45                         } else r = mid-1;
46                     }
47                     if(id!=-1)
48                         ask = max(bns[id]%bns[i],ask);
49                 }
50             }
51             if(ask >= bns[i])break;
52         }
53         printf("%d\n",ask);
54     }
55     return 0;
56 }
				
时间: 2024-08-13 16:28:25

1421 最大MOD值的相关文章

51 nod 1421 最大MOD值

1421 最大MOD值 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含一个整数n,表示数组a的大小.(1 ≤ n ≤ 2*10^5) 第二行有n个用空格分开的整数ai (1 ≤ ai ≤ 10^6). Output 输出一个整数代表最大的mod值. I

51nod 1421 最大MOD值

分析:首先去重排序,然后枚举a[i]的倍数,找到最大的a[j],使得a[j]小于a[i]的倍数,用二分法找,然后更新一下最大值.枚举a[i]和倍数复杂度为O(nlogn),二分O(logn),总的为O(n(logn)^2). 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 const int maxn=200005; 6 int a[maxn]

51nod1421 最大MOD值

O(n2)tle.O(nlognlogn) #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,

51Nod 1004 n^n的末位数字(日常复习快速幂,莫名的有毒,卡mod值)

1004 n^n的末位数字 题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字. Input 一个数N(1 <= N <= 10^9) Output 输出N^N的末位数字 Input示例 13 Output示例 3 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!pr

高速幂小模板

long long quickpow(long long m,long long n) { long long ans=1; while(n) { if(n&1) ans=(ans%mod)*(m%mod)%mod; n=n>>1; m=(m*m)%mod; } return ans; } mod是要求的取mod值.至于矩阵的高速幂就把乘法和计算的对象换为矩阵的即可了.

POJ-2513 Colored Sticks 【欧拉通路+Trie】

Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color? Input Input is a

dz改写CSS

在template下新建mytpl目录 复制default下的discuz_style_default.xml到mytpl下 改写其中 <item id="name"><![CDATA[默认风格]]></item> <item id="templateid"><![CDATA[1]]></item> <item id="tplname"><![CDATA[默

Codeforces 55D Beautiful numbers 数位dp(入门

题目链接:点击打开链接 题意: 我们认为一个数 num 能被每一位上的数字整除(expect 0) 那么这个数num就是合法的. 给出区间[l,r] ,问这个区间内有多少个合法的数. 首先solve(long x) 返回 [0, x] 内的合法个数,答案就是 solve(r) - solve(l-1); 以1234567为例 flag表示当前这位是能任意填,还是只能填<=该位对应的数字 若当前搜索的是第三位,且第二位已经填了0或1,则此时第三位可以任意填. 若第二位填的是2,则第三位只能填 [0

discuz2.5变量大全

<?php   /** * [Discuz!] (C)2001-2099 Comsenz Inc. * This is NOT a freeware, use is subject to license terms * * $Id: lang_admincp.php 31458 2012-08-30 03:39:40Z zhengqingpeng $ */   if(!defined('IN_DISCUZ')) { exit('Access Denied'); }     $lang = arr