Codeforces Round #489 (Div. 2)

Codeforces Round #489 (Div. 2)

A. Nastya and an Array

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define frep(i,a,b) for(int i=a;i>=b;--i)
#define mem(W) memset(W,0,sizeof(W))
#define pb push_back
typedef long long ll;
const int N = 100;
const int inf = 0x3f3f3f3f;
inline int read() {
    char c=getchar(); int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
using namespace std;
int n,ans;
map<int,int> M;
int main() {
    n = read();
    rep(i,1,n){
        int x=read();
        if(x&&!M[x]){
            M[x]=1;
            ++ans;
        }
    }
    printf("%d\n",ans);
    return 0;
}

B. Nastya Studies Informatics

\(gcd(a,b) = x, lcm(a,b) = y\) , 即 \(gcd(\frac {a}{x},\frac {b}{x}) = 1, \frac {a*b}{x} = \frac{a}{x}*b = y\),那么我们可以直接枚举\(\frac{a}{x}\)来检查a,b是否在[l,r]内记入答案,只需要枚举\(\sqrt{y}\)以内的数即可。(卡的太久导致C没时间检查。。。直接凉了
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define frep(i,a,b) for(int i=a;i>=b;--i)
#define mem(W) memset(W,0,sizeof(W))
#define pb push_back
typedef long long ll;
const int N = 100;
const int inf = 0x3f3f3f3f;
inline ll read() {
    char c=getchar(); ll x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
using namespace std;
ll x,y,l,r,ans,ans2;
set< pair<ll,ll> > s;
ll LCM(ll a,ll b){return a*b/__gcd(a,b);}
int main() {
    l=read();r=read();x=read();y=read();
    if(x==y){
        if(l<=x&&x<=r)return puts("1"),0;
        else return puts("0"),0;
    }
    if(y%x||y<x)return puts("0"),0;
    if(y==1){
        if(x==1&&l<=x&&x<=r)return puts("1"),0;
        else return puts("0"),0;
    }

    for(ll i=1;i*i<=y;++i){
        if(y%(x*i)==0&&__gcd(i*x,y/i)==x&&LCM(i*x,y/i)==y){
            if(l<=i*x&&y/i<=r&&l<=y/i&&i*x<=r)s.insert(make_pair(i*x,y/i)),s.insert(make_pair(y/i,i*x));
        }
    }
    printf("%d\n",(int)s.size());

    return 0;
}

C. Nastya and a Wardrobe

稍微展开写一下,可以发现规律,然后公式就很显然了,具体见代码。注意特判。
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define frep(i,a,b) for(int i=a;i>=b;--i)
#define mem(W) memset(W,0,sizeof(W))
#define pb push_back
typedef long long ll;
const int N = 100;
const int inf = 0x3f3f3f3f;
inline int read() {
    char c=getchar(); int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
using namespace std;
const int mod = 1e9 + 7;
ll q_pow(ll a,ll b) {
    ll ans=1;
    while(b){
        if(b&1)ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
ll n,k;
int main() {
    scanf("%I64d%I64d",&n,&k);
    if(n==0)return puts("0"),0;
    n%=mod;
    printf("%I64d\n",((q_pow(2,k+1)*n)%mod-(q_pow(2,k)-1)%mod+mod)%mod);
    return 0;
}

D. Nastya and a Game

\(p = s*k ≤ 2*10^{18}\),所以如果每次乘一个大于1的数,p最多需要\(\log{(2*10^{18})}\)个数相乘。那么只需要按题意枚举左端点,每次跳到下一个非1的位置,对于连续的一段1,计算最大,最小值就可以判断是否包含符合条件的位置。
#include <bits/stdc++.h>
typedef long long ll;
const int N = 2e5 + 100;
using namespace std;
int n,k;
ll a[N],sum[N];
int nxt[N];
void init() {
    for(int i=1;i<=n;++i) sum[i]=sum[i-1]+a[i];
    nxt[n] = n+1;
    for(int i=n-1;i>=1;--i) {
        if(a[i+1] == 1) nxt[i] = nxt[i+1];
        else nxt[i] = i+1;
    }
}
int ck1(ll a, ll b) {return a*1.0 > LONG_MAX*1.0/b;}
int ck(ll x1, ll x2, ll p) {
    if(p%k==0) {
        p/=k;
        if(x1<=p&&p<=x2) return 1;
    }
    return 0;
}
int main() {
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;++i) scanf("%I64d",&a[i]);
    init();
    int ans = 0;
    for(int L=1;L<=n;++L) {
        ll x=1;
        for(int R=L;R<=n;R=nxt[R]) {
            if(ck1(x, a[R])) break;
            x*=a[R];
            if(ck(sum[R]-sum[L-1], sum[nxt[R]-1]-sum[L-1], x)) ++ans;
        }
    }
    printf("%d\n", ans);
}


以后用markdown,然而代码高亮的问题还没解决,所以。。。

原文地址:https://www.cnblogs.com/RRRR-wys/p/9205433.html

时间: 2024-07-30 07:20:34

Codeforces Round #489 (Div. 2)的相关文章

Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans

这道题的算法是: i从1开始,首先求sum(1-i),然后在[i+1, n]中找到第一个a[j]>=sum(1, i) 如果a[j]==sum(1, i)结束搜索,否则令i=j,循环过程 因为每次做完一次之后sum会至少增大一倍,所以一个查询的复杂度会维持到log(Max(a[i])) 需要维护 区间最大值和区间和 的线段树来实现算法 #include <iostream> #include <cstdio> #include <cmath> #include &

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

DP Codeforces Round #303 (Div. 2) C. Woodcutters

题目传送门 1 /* 2 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 3 问最多能砍到多少棵树 4 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 5 分情况讨论,若符合就取最大值更新,线性dp,自己做出来了:) 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #include &