Fifth winter training match

Question A: Ham sub

Title Description

The language to be over the age of four ⽣ Satsuki day, her mother prepared for her legs n roots in fire, she wanted them to catch fire will be divided between the legs of m-bit children, so she may need to cut the legs to catch fire. To save, Satsuki The language ⼑ to chop the minimum number, so that the leg is divided into n number of equal parts in fire m. I will cut at least to get accustomed ⼑?

Entry

First frame ⾏ ⼀ integers T, T set of data expressed.
Next T sets of data, each ⼀ ⾏ total, there are two numbers n, m.

Export

Each set of data ⼀ ⾏, at least to the number of output ⼑ cut.

Sample input Copy

2
2 6
6 2

Sample output Copy

4
0

Ideas :
a title start sausage did not say 2 2 3 portions cut long knife, WA, and then consider this situation, to find the law can be obtained solution;

ll gcd(ll a,ll b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}

int main()
{
    int n;cin>>n;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        if(y==0||x%y==0) cout<<0<<endl;
        else if(y%x==0)
        {
            ll z=y/gcd(x,y);
            cout<<(z-1)*x<<endl;
        }
        else
            cout<<y-gcd(x,y)<<endl;
    }
    return 0;
}

Problem B: wages

Title Description

n + e in the summer to participate in the activities to fight ⼯ zero, this event divided into n ⼯ for the day, every day to work surely ⼯ for funding of Vi. Time settlement of m ⼯ money, n + e can arrange these times, that is when money, the boss said not, n + e have the right to send The language! (Because the n + e is ⼟ Howe, that he is the boss's boss)
+ E n do not like him ⼀-time have much money, so he would like to arrange to get the money to record the next time, so that he ⼀-time money is in that is largest of the smallest. (Last frame given day ⼀ essentials money)

Entry

2 ⾏ first frame number n, m
Next ⾏ n, number per ⾏ ⼀, Vi represents

Export

Output ⼀ ⾏ ⼀ integer representing the smallest from the largest amount of money.

Sample input Copy

7 5
100
400
300
100
500
101
400

Sample output Copy

500

prompt

// 300 100 // 100 400 // 500 101 // 400 // "//" represents a n + e to get the money.

For 20% of the data, 1 <= n <= 20
to 20% of the other data, 1≤n≤50, Vi, and no more than 1000
data for 100%, 1≤n≤100000, m≤n, Vi≤ 100 000

Thinking :
binary answer ANS, is determined mid judge whether the function is determined, is determined in line with the requirements to satisfy packet, r can be reduced or increased to l.

int n,m;
ll a[100005];///用long long

bool judge(ll mid)
{
    int cnt=0;
    ll ret=0;
    for(int i=1;i<=n;i++)
    {
        if(ret+a[i]>mid)
        {
            ret=a[i];
            cnt++;
        }
        else ret+=a[i];
        if(cnt>=m || ret>mid)///有多于m个分组是大于mid的,则非法
            return false;    ///或者是其中一个数ret大于mid,也非法
    }
    if(cnt<m) return true;
    return false;
}

int main()
{
    cin>>n>>m;
    ll ans,minn=1000000,maxn=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        maxn+=a[i];
        minn=min(minn,a[i]);
    }
    while(minn<maxn)
    {
        ll mid=(minn+maxn)/2;
        if(judge(mid)) maxn=mid;
        else minn=mid+1;

    }
    cout<<minn<<endl;
}

Problem C: divisor

Title Description

Saying CD comparison ⽋ flat, he said no date submenus classroom leader hit him in the next day submenus lonely, so it ⼀ night, he finally came to the computer room was hit. Since the CD is zoomed family pet, then zoomed have come home to play the CD.
The computer room there are n Face, the i-Face want to play next CD ai. But too many PORTRAIT hit CD, he would unhappy, so he prescribed only K Face hit him, and to be fair, the final number of K Face hit him must be the same, CD prescribed number of times this is this K Face want to play is largest common divisor of his times. Why is largest common divisor is it? Because he felt that was the number of hits is GCD, then he will become Glad CD.
Said before, CD relatively ⽋ flat, so the CD hope, K Face hit him a number of times and is largest. You can tell him how much his final total will be playing under it?

Entry

First frame ⾏ two positive integers n, k.
Second shot ⾏ n positive integer representing the number of each CD Face hope to play next.

Export

Output ⼀ positive integer CD play will be much lower.

Sample input Copy

3 1
1 2 3

Sample output Copy

3

prompt

For 30% of the data, to ensure k≤n≤20.
For 50% of the data, to ensure that all START input number is less than 5000.
To 100% of the data, to ensure that all START input number is less than 500000, k≤n.

Thinking: Violence Enumeration: i from a [i] _max begins the enumeration to 1, greater than or equal queries whether the number i is a multiple of the number k, and if so, i is the greatest common divisor.

int n;
ll k;
int visit[500005];
int main()
{
    cin>>n>>k;
    int maxn=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);visit[x]++;
        maxn=max(maxn,x);
    }
    for(int i=maxn;i>=1;i--)
    {
        int cnt=0;
        for(int j=i;j<=maxn;j+=i)///查询是否存在i的倍数
        {
            if(visit[j])cnt+=visit[j];///存在,则计算有多少个
            if(cnt>=k) break;
        }
        if(cnt>=k)
        {
            printf("%lld",i*k);///cout会出错
            break;
        }
    }
}
发布了95 篇原创文章 · 获赞 7 · 访问量 8397

Guess you like

Origin blog.csdn.net/Spidy_harker/article/details/104079989