QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#731216#5259. Skills in Pillsjay248TL 0ms0kbC++141.6kb2024-11-10 00:41:542024-11-10 00:41:55

Judging History

你现在查看的是最新测评结果

  • [2024-11-10 00:41:55]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-10 00:41:54]
  • 提交

answer

#include <iostream>
#include <cmath>
using namespace std;


int gcd(int x, int y){
    int tmp;
    if(x<y){
        tmp = x;
        x = y;
        y = tmp;
    }
    while(y>0){
        tmp = y;
        y = x%y;
        x = y;
    }
    return x;
}

int main(){
    int k, j, n;
    int ans;
    int a[1000005];
    int b[1000005];
    cin>>k>>j>>n;
    int cnt1=0;
    int cnt2=0;
    if(gcd(k, j)>1){
        ans = fmin(n/k + (n+1)/j, (n+1)/k + n/j);
    }

    else{
    int x, y;

    x=1;
    while(true){
        if((k*x-1)%j==0) break;
        x++;
    }

    for(int i=1; i<=k*x-1; i++){
        if(i>n) break;
        a[i] = (i+1)/k + i/j;
    }

    y=1;
    while(true){
        if((j*y-1)%k==0) break;
        y++;
    }

    for(int i=1; i<=j*y-1; i++){
        if(i>n) break;
        b[i] = (i+1)/j + i/k;
    }

    if(k*x > y*j){
        for(int i=y*j; i<k*x; i++){
            if(i>n) break;
            b[i] = fmin(b[i-j*y+1] + b[j*y-1], a[i-j*y+1] + b[j*y-1]);
        }
        for(int i=k*x; i<=n; i++){
            a[i] = fmin(a[i-k*x+1] + a[k*x-1], b[i-k*x+1] + a[k*x-1]);
            b[i] = fmin(b[i-j*y+1] + b[j*y-1], a[i-j*y+1] + b[j*y-1]);
        }
    }

    else{
        for(int i=k*x; i<y*j; i++){
            if(i>n) break;
            a[i] = fmin(a[i-k*x+1] + a[k*x-1], b[i-k*x+1] + a[k*x-1]);
        }
        for(int i=y*j; i<=n; i++){
            a[i] = fmin(a[i-k*x+1] + a[k*x-1], b[i-k*x+1] + a[k*x-1]);
            b[i] = fmin(b[i-j*y+1] + b[j*y-1], a[i-j*y+1] + b[j*y-1]);
        }
    }

    ans = k+j+fmin(a[n-j*k], b[n-j*k]);
    }
    cout<<ans<<'\n';

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

3 9 20

output:


result: