【CodeFores 948 】B【筛法+思维】C【树状数组+思维】

48 篇文章 0 订阅
11 篇文章 0 订阅

B. Primal Sport
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try to reach one million by the process described below.

Alice goes first and then they take alternating turns. In the i-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.

Formally, he or she selects a prime p < Xi - 1 and then finds the minimum Xi ≥ Xi - 1 such that p divides Xi. Note that if the selected prime p already divides Xi - 1, then the number does not change.

Eve has witnessed the state of the game after two turns. Given X2, help her determine what is the smallest possible starting number X0. Note that the players don’t necessarily play optimally. You should consider all possible game evolutions.

Input
The input contains a single integer X2 (4 ≤ X2 ≤ 106). It is guaranteed that the integer X2 is composite, that is, is not prime.

Output
Output a single integer — the minimum possible X0.

Examples
inputCopy
14
output
6
inputCopy
20
output
15
inputCopy
8192
output
8191
Note
In the first test, the smallest possible starting number is X0 = 6. One possible course of the game is as follows:

Alice picks prime 5 and announces X1 = 10
Bob picks prime 7 and announces X2 = 14.
In the second case, let X0 = 15.

Alice picks prime 2 and announces X1 = 16
Bob picks prime 5 and announces X2 = 20.

分析:很明显就是要倒着找x0.,从x2找到x1,然后从x1找到x0 .找了半天才找到规律。
p0 x0 —— p1 x1—– x2

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 1e6+11;
const int M = 1e5+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

bool su[N]={1,1,0};
LL mx[N];  // mx[i]表示为i的最大质因子为多少
void init(){
    for(LL i=2;i*i<N;i++){
        if(!su[i]){
            for(LL j=i*i;j<N;j+=i)
                su[j]=1;
        }
    }

    for(LL i=2;i*i<=N;i++){
        for(LL j=1;j*i<=N;j++){
            if(!su[i]) mx[i*j]=max(mx[i*j],i);
            if(!su[j]) mx[i*j]=max(mx[i*j],j);
        }
    }

   // int m=10; for(int i=1;i<=m;i++) printf("%lld \n",mx[i]);
}

int main(){
    init();
    LL x2;
    while(scanf("%lld",&x2)!=EOF){
        LL x0=inf;
        for(int x1=(x2/mx[x2]-1)*mx[x2]+1; x1<=x2 ; x1++){ // 枚举 x1 
            if(!su[x1]) continue;
             x0=min(x0,(x1/mx[x1]-1)*mx[x1]+1);
        }
        printf("%lld\n",x0);
    }
return 0;
}

C. Producing Snow
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice likes snow a lot! Unfortunately, this year’s winter is already over, and she can’t expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.

Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.

Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.

You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.

Input
The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.

The second line contains N integers V1, V2, …, VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.

The third line contains N integers T1, T2, …, TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.

Output
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.

Examples
inputCopy
3
10 10 5
5 7 2
output
5 12 4
inputCopy
5
30 25 20 15 10
9 10 12 4 13
output
9 20 35 11 25
Note
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.

分析:乍一看很像线段树,但是仔想一下就知道,肯定是不行的。我们可以想一下,第i天的时候其 融化雪的贡献怎么算的 ,假设第i天前面 有 k天可以 融化到今天并且还不会融化完,其余的天 在当天肯定是无法融化T[i] 的雪的,那么第i天可以融化的雪为 k * T[i] + cnt(在第i天无法融化T[i]雪,但是肯定会融化一些雪的总量)。 然后结果就很明显了,考虑每一天制造的雪可以在哪天化完就行了。 [有了这个思路就很容易 写了,之后的详细步骤看代码吧]
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 1e5+11;
const int M = 1e6+11;

int c[N]; int n;
int lowbit(int x){return x&(-x); }
void add(int x,int val){
    for(int i=x;i<=n;i+=lowbit(i)) c[i]+=val;
}
int query(int x){
    int ans=0;
    for(int i=x;i>=1;i-=lowbit(i)) ans+=c[i];
    return ans;
}

LL V[N],T[N];
LL sum[N],cnt[N];
int main(){
     scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&V[i]);
    }
    for(int i=1;i<=n;i++) {
        scanf("%lld",&T[i]);
        sum[i]=sum[i-1]+T[i];
    }
    for(int i=1;i<=n;i++){
        int L=i;
        int R=upper_bound(sum,sum+n+1,V[L]+sum[L-1])-sum;
        if(R==n+1) {
            add(L,1);
            add(R,-1);
        }else {
            cnt[R]+=V[L]+sum[L-1]-sum[R-1];
            add(L,1);
            add(R,-1);
        }
    }

    for(int i=1;i<=n;i++){
        if(i!=1) putchar(' ');
        printf("%lld",query(i)*T[i]+cnt[i]);
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值