Codeforces 1166

1166 D

题意

我们说一个序列 \(x\) (长为 \(n\) )是 \(m-cute\) 的当且仅当 \(x_i = x_{i - 1} + x_{i - 2} + \dots + x_1 + r_i (1 \le r_i \le m)\) 。现在给定 \(x\) 的首项和末项以及 \(m\) ,问是否存在合法的 \(x\) 。存在还需要构造一个。 \((x_1,x_n,m\le 10^{14})\)

Example

input
2
5 26 2
3 9 1
output
4 5 6 13 26
-1

先假定所有的 \(r_i=1\) ,算出 \(n\) ,然后再从左往右一个个往上加

Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=53;
long long a[maxn],c[maxn],s,t,m;
int n;
void calcn(){
    n=2;
    long long sum=s;
    a[1]=s;
    while(1){
        a[n]=sum+1;
        if(a[n]>t)break;
        sum+=a[n];
        n++;
    }
    n--;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&s,&t,&m);
        calcn();
        for(int i=1;i<=n;i++)c[i]=0;
        long long x=t-a[n],y,k;
        for(int i=2;i<=n&&x>0;i++){
            k=(i==n?1:1ll<<(n-i-1));
            y=min(m-1,x/k);
            x-=y*k;
            c[i]+=y;
        }
        long long sum=s;
        a[1]=s;
        for(int i=2;i<=n;i++){
            a[i]=sum+1+c[i];
            sum+=a[i];
        }
        if(a[n]==t){
            printf("%d ",n);
            for(int i=1;i<=n;i++)printf("%lld ",a[i]);
            puts("");
        }
        else puts("-1");
    }
    return 0;
}

1166 E

题意

猜你喜欢

转载自www.cnblogs.com/BlogOfchc1234567890/p/11041686.html