QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#731198#5259. Skills in Pillsjay248TL 0ms0kbC++141.5kb2024-11-10 00:38:232024-11-10 00:38:24

Judging History

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

  • [2024-11-10 00:38:24]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-10 00:38:23]
  • 提交

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++){
        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++){
        b[i] = (i+1)/j + i/k;
    }
    if(k*x > y*j){
        for(int i=y*j; i<k*x; i++){
            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++){
            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';

}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

3 9 20

output:


result: