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 ...
随机推荐
- gcc static静态编译选项提示错误修正(/usr/lib/ld: cannot find -lc)
用gcc静态编译C程序时显示出: /usr/lib/ld: cannot find -lc /usr/lib/ld: cannot find -lgcc_s /usr/lib/ld: cannot f ...
- Linux定时任务出现问题时正确的解决步骤
但凡是提供服务的,都要有本账.软件服务也不例外.无论是Apache,Nginx,还是我们自己搭建的网站,日志是标配.这里的日志就是一本账. 当定时任务出现问题时,正确的处理步骤是: 1,定时任务服务是 ...
- 机器学习理论基础学习16---高斯网络(GN)
一.高斯网络(高斯图模型)总体介绍 概率图模型分为三种:贝叶斯网络,马尔科夫随机场以及高斯网络:而高斯网络又可以根据有向无向细分为高斯贝叶斯网络和高斯马尔科夫随机场 二.高斯贝叶斯网络 1.高斯贝叶斯 ...
- 面经:Bloomberg Internship第一轮
上来先问了一个系统设计的问题,一个front end, 一个back end. front end有很多UI,一个UI对10个多customers,back end有许多processor,或者pro ...
- js里实现给数字加三位一逗号间隔的两种方法
方法一: <script type= "text/javascript"> var num_s = "1232134456.546 ";ale ...
- LINQ的左连接、右连接、内连接和Lamda表达式实现Left join
1.左连接: var LeftJoin = from t1 in l1join t2 in l2on t1.ID equals t2.ID into Joinedt12from t3 in Joine ...
- POJ 1182 并查集
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- HDU 4770
这题说的是一在一个N*M的房间内,然后有些房间不能被灯光照亮,有一个灯可以转动方向,其他的灯只能在固定一个方向上,因为数据比较小,所以比较水,直接暴力的进行枚举就好了,但是还是 wa了很久,原因没认真 ...
- Python2 和Python3 的差异总结
一.基本语法差异 1.1 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3 ...
- thinkphp标签实现bootsrtap轮播carousel实例
thinkphp标签实现bootsrtap轮播carousel实例由于轮播carousel第一个div需要设置active样式才能正常显示,上面的圆点也同样需要数字,使用volist标签在循环的同时可 ...