Edu CF 103 Div. 2 (A. K-divisible Sum, B. Inflation贪心),被黑客攻了,,惨掉rank, 思维除法与取余, 不太擅长的类型

2021-01-29

题目链接: Educational Codeforces Round 103 (Rated for Div. 2)

题目

A. K-divisible Sum

You are given two integers nn and kk.

You should create an array of npositive integers a1,a2,,ana1,a2,…,an such that the sum (a1+a2++an)(a1+a2+⋯+an) is divisible by kk and maximum element in aa is minimum possible.

What is the minimum possible maximum element in aa?

Input

The first line contains a single integer tt (1t10001≤t≤1000) — the number of test cases.

The first and only line of each test case contains two integers nn and kk (1n1091≤n≤109; 1k1091≤k≤109).

Output

For each test case, print one integer — the minimum possible maximum element in array aa such that the sum (a1++an)(a1+⋯+an) is divisible by kk.

Example
input
Copy
4
1 5
4 3
8 8
8 17
output
Copy
5
2
1
3
Note

In the first test case n=1n=1, so the array consists of one element a1a1 and if we make a1=5a1=5 it will be divisible by k=5k=5 and the minimum possible.

In the second test case, we can create array a=[1,2,1,2]a=[1,2,1,2]. The sum is divisible by k=3k=3 and the maximum is equal to 22.

In the third test case, we can create array a=[1,1,1,1,1,1,1,1]a=[1,1,1,1,1,1,1,1]. The sum is divisible by k=8k=8 and the maximum is equal to 11.

 


 

思路:

大致看来,分三个情况: (带==>就是说的结果)

    1. n=k, ==>1

    2. n<k,数大被分的少 ,  结果就是, (k-1) / 2 + 1 

     比如9~16被分为8份==>2;   17~24被分8份==>3  

 

    3. n>k, 数小被分的多, 这里还分两种情况

       1). n是k的整倍数 ==> 1,  可以与第一种情况合并.

     2). else ==>2 比如 n = 3时, k=4  ->1, 1, 2, 2;   

                    k=5 ->1, 1, 2, 2, 3  把最后的3补到前面->1, 2, 2, 2, 2 化简-> 1, 1, 1, 1, 2

                 k=6, ==> n是k的倍数 ==>1

      总而言之, 1和2就能搞定, 所以结果==>2

AC代码

#include <iostream>

using namespace std;int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int n, num;
        cin >> n >> num;
        
        if(n % num == 0)
            cout << 1 << endl;
        else
        {
            int ans;
            if(n > num)
                ans = 2;
            else
                ans = (num + n - 1) / n;
            cout << ans << endl;
        }
        cout << endl;
    }
    return 0;
}

 

 

 


 

 
B. Inflation(贪心)

You have a statistic of price changes for one product represented as an array of nn positive integers p0,p1,,pn1p0,p1,…,pn−1, where p0p0 is the initial price of the product and pipi is how the price was increased during the ii-th month.

Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase pipi to the price at the start of this month (p0+p1++pi1)(p0+p1+⋯+pi−1).

Your boss said you clearly that the inflation coefficients must not exceed kk %, so you decided to increase some values pipi in such a way, that all pipi remain integers and the inflation coefficients for each month don't exceed kk %.

You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the total sum of changes.

What's the minimum total sum of changes you need to make all inflation coefficients not more than kk %?

Input

The first line contains a single integer tt (1t10001≤t≤1000) — the number of test cases.

The first line of each test case contains two integers nn and kk (2n1002≤n≤100; 1k1001≤k≤100) — the length of array pp and coefficient kk.

The second line of each test case contains nn integers p0,p1,,pn1p0,p1,…,pn−1 (1pi1091≤pi≤109) — the array pp.

Output

For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than kk %.

Example
input
Copy
2
4 1
20100 1 202 202
3 100
1 1 1
output
Copy
99
0
Note

In the first test case, you can, for example, increase p0p0 by 5050 and p1p1 by 4949 and get array [20150,50,202,202][20150,50,202,202]. Then you get the next inflation coefficients:

  1. 502015011005020150≤1100;
  2. 20220150+50110020220150+50≤1100;
  3. 20220200+202110020220200+202≤1100;

 

In the second test case, you don't need to modify array pp, since the inflation coefficients are already good:

  1. 1110010011≤100100;
  2. 11+110010011+1≤100100;

题意:

从a2开始,每个值的计算方式为:pi=ai/(a0+a1+a2+...+ai-1)

使得每个pi都<=k/100的最少增加值之和

 可惜啊, 做了一半困得没再做, 当时代码出了点bug, 点写在代码里了

AC代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll p[110], bottom, top, res;

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int n, k;
        res = 0;
        cin >> n >> k;
        for(int i = 0 ; i< n; i ++)
            cin >> p[i];
        
        bottom = 0;
        for(int i = 0; i < n - 1; i ++)
        {
            bottom += p[i];
            top = p[i + 1];
            if(bottom * k < 100 * top)
            {
                ll d = 100 * top / k - bottom;//这里切记用long long
                if(100 * top % k != 0)//有余数+1
                    d ++;
                res += d;
                bottom += d;    
            }    
        }
            cout << res << endl;    
    }
    return 0;
}

 

 

 

 

posted @ 2021-02-01 16:15  la-la-wanf  阅读(108)  评论(0编辑  收藏  举报