Codeforces Round #489 (Div. 2)
1 second
256 megabytes
standard input
standard output
Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:
- In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero.
- When all elements of the array become equal to zero, the array explodes.
Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the size of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−105≤ai≤105−105≤ai≤105) — the elements of the array.
判除0外不同数字的个数。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define mod 1000000007
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=1e5+;
set<LL> num;
int a[N];
int n;
int main()
{
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
if(a[i]==) continue;
num.insert(a[i]);
}
printf("%d\n",num.size());
return ;
}
1 second
256 megabytes
standard input
standard output
Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.
We define a pair of integers (a,b)(a,b) good, if GCD(a,b)=xGCD(a,b)=x and LCM(a,b)=yLCM(a,b)=y, where GCD(a,b)GCD(a,b) denotes the greatest common divisor of aa and bb, and LCM(a,b)LCM(a,b) denotes the least common multiple of aa and bb.
You are given two integers xx and yy. You are to find the number of good pairs of integers (a,b)(a,b) such that l≤a,b≤rl≤a,b≤r. Note that pairs (a,b)(a,b) and (b,a)(b,a) are considered different if a≠ba≠b.
The only line contains four integers l,r,x,yl,r,x,y (1≤l≤r≤1091≤l≤r≤109, 1≤x≤y≤1091≤x≤y≤109).
In the only line print the only integer — the answer for the problem.
1 2 1 2
2
1 12 1 12
4
50 100 3 30
0
In the first example there are two suitable good pairs of integers (a,b)(a,b): (1,2)(1,2) and (2,1)(2,1).
In the second example there are four suitable good pairs of integers (a,b)(a,b): (1,12)(1,12), (12,1)(12,1), (3,4)(3,4) and (4,3)(4,3).
In the third example there are good pairs of integers, for example, (3,30)(3,30), but none of them fits the condition l≤a,b≤rl≤a,b≤r.
在sqrt(y) 范围内把所有可能的因数暴力找出来,然后暴力跑一遍这所有因数p和lcm/p看他们符不符合 $ p \ge l ,y/p \ge l $ 以及 $ p \le r , y/p \le r $ 并且 $ gcd(p,ly/p)==x $ 就行。符合就ans++。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define mod 1000000007
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=4e4+;
const int P=4e4;
typedef pair<LL,LL> pll;
int inf[N],pri[N];
int tot;
void init()
{
tot=;
for(int i=;i<=P;i++)
{
if(!inf[i])
pri[++tot]=i;
for(int j=;j<=tot && pri[j]*i<=P;j++)
{
inf[i*pri[j]]=;
if(i%pri[j]==) break;
}
}
return ;
}
set<LL> dive;
set<pll> ans;
int l,r,lcm,gcd;
bool infer(LL t)
{
return t>=l && t<=r;
}
int num[N];
int main()
{
scanf("%d%d%d%d",&l,&r,&gcd,&lcm);
for(LL d=;d*d<=lcm;d++)
if(lcm%d==)
dive.insert(d),dive.insert(lcm/d);
for(auto p:dive)
{
LL xx=gcd*p,yy=lcm/p;
if(infer(xx) && infer(yy) && __gcd(xx,yy)==gcd) ans.insert(mp(xx,yy));
}
printf("%d\n",ans.size());
return ;
}
1 second
256 megabytes
standard input
standard output
Nastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).
Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50%50% probability. It happens every month except the last one in the year.
Nastya owns xx dresses now, so she became interested in the expected number of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for k+1k+1 months.
Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 109+7109+7, because it is easy to see that it is always integer.
The only line contains two integers xx and kk (0≤x,k≤10180≤x,k≤1018), where xx is the initial number of dresses and k+1k+1 is the number of months in a year in Byteland.
In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109+7109+7.
2 0
4
2 1
7
3 2
21
In the first example a year consists on only one month, so the wardrobe does not eat dresses at all.
In the second example after the first month there are 33 dresses with 50%50% probability and 44 dresses with 50%50% probability. Thus, in the end of the year there are 66 dresses with 50%50% probability and 88 dresses with 50%50% probability. This way the answer for this test is (6+8)/2=7(6+8)/2=7.
把期望计算式列出来可以发现这个答案就是一个式子。注意一下n的大小处理。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define mod 1000000007
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
using namespace std;
LL n,k;
LL quickpow(LL x,LL n)
{
LL ans=;
x%=mod;
while(n)
{
if(n&)
ans=ans*x%mod;
x=x*x%mod;
n>>=;
}
return ans%mod;
}
int main()
{
cin>>n>>k;
if(n==)
printf("0\n");
else
{
n%=mod;
LL ans=((quickpow(,k+)*n%mod-(quickpow(,k)-+mod)%mod)%mod+mod)%mod;
ans%=mod;
printf("%I64d\n",ans);
}
return ;
}
2 seconds
256 megabytes
standard input
standard output
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that ps=kps=k, where pp is the product of all integers on the given array, ss is their sum, and kk is a given constant for all subsegments.
Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.
The first line contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1051≤k≤105), where nn is the length of the array and kk is the constant described above.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1081≤ai≤108) — the elements of the array.
In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to kk.
1 1
1
1
4 2
6 3 8 1
2
In the first example the only subsegment is [1][1]. The sum equals 11, the product equals 11, so it suits us because 11=111=1.
There are two situable subsegments in the second example — [6,3][6,3] and [3,8,1][3,8,1]. Subsegment [6,3][6,3] has sum 99 and product 1818, so it suits us because 189=2189=2. Subsegment [3,8,1][3,8,1] has sum 1212 and product 2424, so it suits us because 2412=22412=2.
我们看到数据范围,可以发现k*sum<= 2e18, 因此乘积不可能大于2e18,一旦大于往后就没有匹配线段了。我们枚举所有线段左端点,然后往后暴力跳右端点。我们每次跳的位置是一个非1的位置,这样最多跳60次。每次跳的范围内最多产生1个答案贡献。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=2e5+;
const LL eps=1e18;
typedef pair<LL,LL> pll;
LL a[N];
LL aft[N];
LL ans;
int main()
{
int n;
LL k;
scanf("%d",&n);
scanf("%I64d",&k);
ans=;
for(int i=;i<=n;i++)
scanf("%I64d",a+i);
aft[n+]=n+;
aft[n]=n;
for(int i=n-;i>=;i--)
if(a[i]>) aft[i]=i;
else aft[i]=aft[i+];
LL sum,mul;
int pre,suf;
for(int i=;i<=n;i++)
{
sum=;
mul=;
suf=i;
pre=i-;
while(suf<=n)
{
sum+=a[suf];
if(eps/a[suf]<mul)
break;
mul*=a[suf];
pre=suf;
suf=aft[suf+];
if(mul%k== && mul/k>=sum && mul/k<=sum+suf-pre-)
ans++;
sum+=suf-pre-;
}
}
printf("%I64d\n",ans);
return ;
}
3 seconds
256 megabytes
standard input
standard output
Nastya likes reading and even spends whole days in a library sometimes. Today she found a chronicle of Byteland in the library, and it stated that there lived shamans long time ago. It is known that at every moment there was exactly one shaman in Byteland, and there were nn shamans in total enumerated with integers from 11 to nn in the order they lived. Also, each shaman had a magic power which can now be expressed as an integer.
The chronicle includes a list of powers of the nn shamans. Also, some shamans can be king-shamans, if they gathered all the power of their predecessors, i.e. their power is exactly the sum of powers of all previous shamans. Nastya is interested in whether there was at least one king-shaman in Byteland.
Unfortunately many of the powers are unreadable in the list, so Nastya is doing the following:
- Initially she supposes some power for each shaman.
- After that she changes the power of some shaman qq times (the shamans can differ) and after that wants to check if there is at least one king-shaman in the list. If yes, she wants to know the index of any king-shaman.
Unfortunately the list is too large and Nastya wants you to help her.
The first line contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105).
The second line contains nn integers a1,…,ana1,…,an (0≤ai≤1090≤ai≤109), where aiai is the magic power of the ii-th shaman.
After that qq lines follow, the ii-th of them contains two integers pipi and xixi (1≤pi≤n1≤pi≤n, 0≤xi≤1090≤xi≤109) that mean that the new power of the pipi-th shaman is xixi.
Print qq lines, the ii-th of them should contain −1−1, if after the ii-th change there are no shaman-kings, and otherwise a single integer jj, where jj is an index of some king-shaman after the ii-th change.
If there are multiple king-shamans after each change, print the index of any of them.
2 1
1 3
1 2
-1
3 4
2 2 3
1 1
1 2
2 4
3 6
3
2
-1
3
10 7
0 3 1 4 6 2 7 8 10 1
2 5
1 3
9 36
4 10
4 9
1 2
1 0
1
-1
9
-1
4
-1
1
In the first example powers of shamans after the first change are equal to (2,3)(2,3). The answer equals −1−1, because the sum of powers of shamans before the first shaman is equal to 00, and before the second is equal to 22.
In the second example after the first change the powers are equal to (1,2,3)(1,2,3). The answer is equal to 33, because the power of the third shaman is equal to 33, and the sum of powers of the first and the second shaman is also 1+2=31+2=3. After the second change the powers become equal to (2,2,3)(2,2,3), where the answer equals 22. After the third change the powers become equal to (2,4,3)(2,4,3), where the answer equals −1−1. After the fourth change the powers become equal to (2,4,6)(2,4,6), where the answer equals 33.
我们用bit存前缀和。然后我们在询问修改后,从tmp=a[1]开始,每次寻找一个位置t,它的前缀和大小pre不超过tmp*2-1,然后看t+1位置符不符合a[t+1] == pre[t] ,不符合则修改tmp为pre[t+1]继续倍增。由于我们要寻找的位置是a[p] == pre[p-1] 的这个p位置,因此这个倍增能很快速地在log2(n)*log2(n)的复杂度找到这个p位置。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=(<<)+;
const int maxn=<<;
int n,q;
LL bits[N];
LL a[N];
void add(int i,LL x)
{
while(i<=maxn)
{
bits[i]+=x;
i += i & -i;
}
return ;
}
LL sum(int i)
{
LL ans=;
while(i)
{
ans+=bits[i];
i -= i & -i;
}
return ans;
}
int upt(LL x)
{
if(bits[maxn]<=x) return INF;
int p=;
LL res=;
for(int i=;i>=;i--)
if(res+bits[p|(<<i)]<=x)
{
res+=bits[p|(<<i)];
p|=(<<i);
}
return p+;
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%I64d",a+i);
add(i,a[i]);
}
while(q--)
{
int pos;
LL val;
scanf("%d%I64d",&pos,&val);
add(pos,val-a[pos]);
a[pos]=val;
LL tmp=a[];
LL ans=-;
if(tmp==)
{
printf("1\n");
continue;
}
while(tmp<INF)
{
int k=upt(*tmp-);
if(k==INF)
break;
if(sum(k-)==a[k])
{
ans=k;
break;
}
tmp=sum(k-)+a[k];
}
printf("%I64d\n",ans);
}
return ;
}
Codeforces Round #489 (Div. 2)的更多相关文章
- 【伪暴力+智商剪枝】Codeforces Round #489 (Div. 2) D
失踪人口突然回归……orz.题解还是有必要写的,虽然估计只有自己(?自己也不一定看得懂)看得懂. 题目链接:http://codeforces.com/contest/992/problem/D 题目 ...
- Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans(线段树)
题意 给出一个长度为 \(n\) 的序列 \(\{a_i\}\) , 现在会进行 \(m\) 次操作 , 每次操作会修改某个 \(a_i\) 的值 , 在每次操作完后你需要判断是否存在一个位置 \(i ...
- Codeforces Round #489 (Div. 2) E - Nastya and King-Shamans
E - Nastya and King-Shamans 题目大意:有n个数,每一次操作更改一个数,每次操作之后问你是否有一个数等于其前面所有数的和. 思路:好题,想了很久没想出来,看了题解,主要思想就 ...
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- [Codeforces]Codeforces Round #489 (Div. 2)
Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- UNIX网络编程 第8章 基本UDP套接字编程
UDP是无连接的,不需要accept,TCP通过accept API来接受连接,并且将连接客户端的信息写入到accept将返回的新socket中,该新socket中有服务端和客户端的IP地址和端口,因 ...
- Java并发编程(4)--生产者与消费者模式介绍
一.前言 这种模式在生活是最常见的,那么它的场景是什么样的呢? 下面是我假象的,假设有一个仓库,仓库有一个生产者和一个消费者,消费者过来消费的时候会检测仓库中是否有库存,如果没有了则等待生产,如果有就 ...
- 20165227朱越 预备作业3 Linux安装及学习
预备作业3 Linux安装及学习 Linux的安装 虚拟机的安装远没有想象中的那样容易,下载还没有出现什么问题,当我安装的时候,第一个问题出现在创建虚拟机时选择安装的虚拟机版本和类型的时候的错误 当时 ...
- u-boot移植随笔(7):u-boot启动流程简图【转】
转自:http://www.latelee.org/porting-uboot/u-boot-porting-bootstrap.html u-boot移植随笔:u-boot启动流程简图 画上面这张图 ...
- mysql备份的 三种方式【转】
备份的本质就是将数据集另存一个副本,但是原数据会不停的发生变化,所以利用备份只能回复到数据变化之前的数据.那变化之后的呢?所以制定一个好的备份策略很重要. 一.备份的目的 做灾难恢复:对损坏的数据进行 ...
- LINUX的DNS设置【转】
网卡DNS设置 用windos系统大家都知道,本地连接里面有一个DNS设置. 那么这个选项对应Linux系统的话就是在网卡配置文件,通过编辑vi /etc/sysconfig/network-scr ...
- C# 所生成项目的处理器架构“MSIL”与引用“Oracle.DataAccess, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86”的处理器架构“x86”不匹配。这种不匹配可能会导致运行时失败。
这个问题一般都是Oracle.DataAccess的版本不兼容问题造成的. 解决办法: 1.把Oracle.DataAccess.dll文件拿到C盘或D盘的安装文件的地方进行搜索. 2.会出现在pro ...
- springmvc与struts的区别
一.拦截机制 1.Struts2 a.Struts2框架是类级别的拦截,每次请求就会创建一个Action,和Spring整合时Struts2的ActionBean注入作用域是原型模式prototype ...
- 项目中遇到的问题:Gradle传递性依赖冲突
问题描述: 在调用别人接口时,由于他们接口做了拦截处理在使用RestTemplate调用时必须要使用@Qualifier("他们封装好的类"),需要导入jar包 gradle方式导 ...
- java 读取配置文件类
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...