spoj TBATTLE 质因数分解+二分
题目链接:点击传送
TBATTLE - Thor vs Frost Giants
Thor is caught up in a fierce battle with Loki's army. This army consists of frost giants that have magical powers with them. Their strength levels gets multiplied when they are together. Giants are not highly skilled in the arts of combat, but their sheer size and strength make them formidable opponents even for the Asgardian gods. Thor is no exception. They recover very fast from physical injury but their recovery slows down when they are exposed to extreme heat.
Thor's hammer can generate heat only in multiples of heat quantum N. Frost giants get killed only when their combined strength level is exactly equal to the heat level of the hammer. Thor is interested in killing a continuous stretch of frost enemies with a throw of his hammer with a preference to kill closer enemies first.
Continuous stretch is defined as a set of consecutive elements.
Help Thor to determine the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index. If there is no such continuous stretch possible then print -1.
Input
The first line will contain N, the number of Frost Giants in Loki's army and the Heat quantum.
The second line will contain N integers (a_0, a_2....., a_n-1) - the strength of each frost giant.
Minimum stretch of the army should be 1.
- 1 ≤ N ≤ 100000
- 1 ≤ a_i ≤ 100000
Output
Output the range of the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index.
If there is no such continuous stretch possible then print -1.
Example
Input:
3
1 2 9
Output:
2 2 Input:
5
2 3 4 8 9
Output:
-1 Input:
10
2 4 3 5 17 4 7 5 2 15
Output:
7 8
Explanation
Input #1:
Thor can only kill the stretch [2,2] as this is the minimum length range with strength, multiple of 3.
Input #2:
There is no stretch of frost giants that have combined strength as a multiple of 5.
Input #3:
There are many stretches of frost giants that have strength as multiple of 10. But the minimal stretch with the least indices is from [7,8]. Minimum size stretches are [7, 8] and [8, 9]. Out of them we select [7,8].
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=1e9+;
/// 数组大小
vector<int>p;
int n;
int c[];
void init(int n)
{
memset(c,,sizeof(c));
int si=;
for(int i=;i<=n;i++)
{
if(n%i==)p.push_back(i),si++;
while(n%i==)
{
c[si-]++;
n/=i;
}
}
}
int sum[N][];
int check(int l,int r)
{
for(int j=;j<p.size();j++)
{
if(sum[r][j]-sum[l-][j]<c[j])
return ;
}
return ;
}
int main()
{
while(~scanf("%d",&n))
{
memset(sum,,sizeof(sum));
p.clear();
init(n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
int num=x;
for(int j=;j<p.size();j++)
{
sum[i][j]=sum[i-][j];
while(num%p[j]==)
{
num/=p[j];
sum[i][j]++;
}
}
}
int s=-,e=1e9;
for(int i=;i<=n;i++)
{
int st=i,en=n,ans=-;
while(st<=en)
{
int mid=(st+en)>>;
if(check(i,mid))
{
ans=mid;
en=mid-;
}
else
st=mid+;
}
if(ans!=-)
{
if(ans-i<e-s)
s=i,e=ans;
}
}
if(s==-)
printf("-1\n");
else
printf("%d %d\n",s-,e-);
}
return ;
}
spoj TBATTLE 质因数分解+二分的更多相关文章
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- 2018.09.11 poj1845Sumdiv(质因数分解+二分求数列和)
传送门 显然需要先求出ab" role="presentation" style="position: relative;">abab的所有质因 ...
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
- 整数分解 && 质因数分解
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- POJ1365 - Prime Land(质因数分解)
题目大意 给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式 题解 预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解....其实主要就是字符 ...
- 数学概念——J - 数论,质因数分解
J - 数论,质因数分解 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- 用python与文件进行交互
一.文件处理 1.介绍 计算机系统:计算机硬件,操作系统,应用程序 应用程序无法直接操作硬件,通过操作系统来操作文件,进而读/写硬件中的文件. python打开文件过程: #打开 f=open('a. ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON TestRegionPoint2
zw版[转发·台湾nvp系列Delphi例程]HALCON TestRegionPoint2 procedure TForm1.Button1Click(Sender: TObject);var op ...
- 浅谈EM算法的两个理解角度
http://blog.csdn.net/xmu_jupiter/article/details/50936177 最近在写毕业论文,由于EM算法在我的研究方向中经常用到,所以把相关的资料又拿出来看了 ...
- EditPlus 4.3.2499 中文版已经发布(11月21日更新)
新的版本修复了如下问题: 文本库的日期快捷方式“^@”失效. 列选模式下“减少缩进量”命令无法执行. 在某些情况下突出显示匹配括号导致程序崩溃.(这个问题是我发现的,电邮告诉作者后,一天之内就修复了) ...
- 待解决:PDF header signature not found
- 小试---EF5.0简介
简介 实体框架Entity Framework 是 ADO.NET 中的一组支持开发面向数据的软件应用程序的技术.是微软的一个ORM框架.简单的说就是把关系型数据库映射成面向对象模型. 一篇更加详细的 ...
- jQuery ajax 请求HttpServlet返回[HTTP/1.1 405 Method not allowed]
1.问题使用jQuery的ajax请求 Servlet 时,返回没有进入ajax的success回调函数,浏览器控制台显示 [HTTP/1.1 405 Method not allowed]. 2.解 ...
- Open-Falcon
A Distributed and High-Performance Monitoring System Scalability Scalable monitoring system is neces ...
- 解读 Q_D, Q_Q 指针
见 qglog.h文件定义: #define Q_D(Class) Class##Private * const d = d_func() #define Q_Q(Class) Class * ...
- 谷歌笔试题--给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
谷歌笔试题--给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数. Google2009华南地 ...