Codeforces Round #236 (Div. 2)

A. Nuts

You have a nuts and lots of boxes. The boxes have a wonderful feature: if you put x (x ≥ 0) divisors (the spacial bars that can divide a box) to it, you get a box, divided into x + 1 sections.

You are minimalist. Therefore, on the one hand, you are against dividing some box into more than k sections. On the other hand, you are against putting more than v nuts into some section of the box. What is the minimum number of boxes you have to use if you want to put all the nuts in boxes, and you have b divisors?

Please note that you need to minimize the number of used boxes, not sections. You do not have to minimize the number of used divisors.

Input

The first line contains four space-separated integers k, a, b, v (2 ≤ k ≤ 1000; 1 ≤ a, b, v ≤ 1000) — the maximum number of sections in the box, the number of nuts, the number of divisors and the capacity of each section of the box.

Output

Print a single integer — the answer to the problem.

Sample test(s)
Input
3 10 3 3
Output
2
Input
3 10 1 3
Output
3
Input
100 100 1 1000
Output
1
Note

In the first sample you can act like this:

  • Put two divisors to the first box. Now the first box has three sections and we can put three nuts into each section. Overall, the first box will have nine nuts.
  • Do not put any divisors into the second box. Thus, the second box has one section for the last nut.

In the end we've put all the ten nuts into boxes.

The second sample is different as we have exactly one divisor and we put it to the first box. The next two boxes will have one section each.

#include
#include
#include
#include
#include
#include
using namespace std;

int main()
{

   // freopen("in.in","r",stdin);
    int k,a,b,v;
    scanf("%d%d%d%d",&k,&a,&b,&v);
    int num=a/v+ (a%v==0?0:1);
    int ans=0;
    while((b>=k-1)&&num>0)
    {
        b=b-k+1;
        num-=k;
        ans++;
    }
    if(num>0)
    {
        num-=(b+1);
        ans++;
    }
    if(num>0) ans+=num;
    printf("%d\n",ans);


     return 0;
}

B. Trees in a Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n), ai + 1 - ai = k, where k is the number the Queen chose.

Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?

Input

The first line contains two space-separated integers: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the heights of the trees in the row.

Output

In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions.

If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line "- j x".

If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.

Sample test(s)
Input
4 1
1 2 1 5
Output
2
+ 3 2
- 4 1
Input
4 1
1 2 3 4
Output
0
#include
#include
#include
#include
#include
#include
using namespace std;

int a[1010];
const int INF=0xfffffff;

int main()
{

    // freopen("in.in","r",stdin);
      int n,k;
      while(~scanf("%d%d",&n,&k))
      {
          for(int i=1;i<=n;i++) scanf("%d",&a[i]);
          int nn=INF;
          int st=1;
          int cur=0,tp;
          for(int i=1;i<=n;i++)
          {
              tp=a[i]-k*(i-1);
              if(tp<=0) continue;
              cur=0;
              for(int j=1;j<=n;j++)
              {
                  if(tp!=a[j]) cur++;
                  tp+=k;
              }
              if(nn>cur) st=i,nn=cur;
           }
           printf("%d\n",nn);
           tp=a[st]-k*st;
           int x;
           for(int i=1;i<=n;i++)
           {
               tp+=k;
               if(tp==a[i]) continue;
               x=a[i]-tp;
               if(x>0)
               {
                   printf("- ");
                   printf("%d %d\n",i,x);
               }
               else
               {
                   printf("+ ");
                   printf("%d %d\n",i,-x);
               }
           }

      }
     return 0;
}
C. Searching for Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:

  • the graph contains exactly 2n + p edges;
  • the graph doesn't contain self-loops and multiple edges;
  • for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.

A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.

Your task is to find a p-interesting graph consisting of n vertices.

Input

The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; ) — the number of vertices in the graph and the interest value for the appropriate test.

It is guaranteed that the required graph exists.

Output

For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.

Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.

Sample test(s)
Input
1
6 0
Output
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6

#include
#include
#include
#include
#include
#include
using namespace std;

struct node
{
    int a,b;
};
node ans[500];
int main()
{

     //freopen("in.in","r",stdin);
     int T;
     scanf("%d",&T);
     while(T--)
     {
         int n,p;
         scanf("%d%d",&n,&p);
          int l=0;
          for(int i=1;i<=n;i++)
             {
                 for(int j=i+1;j<=n;j++)
                 {
                     ans[l].a=i;
                     ans[l++].b=j;
                 }
             }
             int num=2*n+p;
             for(int i=0;i<num;i++)
                 printf("%d %d\n",ans[i].a,ans[i].b);

     }
     return 0;
}
D. Upgrading Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have an array of positive integers a[1], a[2], ..., a[n] and a set of bad prime numbers b1, b2, ..., bm. The prime numbers that do not occur in the set b are considered good. The beauty of array a is the sum , where function f(s) is determined as follows:

  • f(1) = 0;
  • Let's assume that p is the minimum prime divisor of s. If p is a good prime, then , otherwise .

You are allowed to perform an arbitrary (probably zero) number of operations to improve array a. The operation of improvement is the following sequence of actions:

  • Choose some number r (1 ≤ r ≤ n) and calculate the value g = GCD(a[1], a[2], ..., a[r]).
  • Apply the assignments: , , ..., .

What is the maximum beauty of the array you can get?

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 5000) showing how many numbers are in the array and how many bad prime numbers there are.

The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — array a. The third line contains m space-separated integers b1, b2, ..., bm (2 ≤ b1 < b2 < ... < bm ≤ 109) — the set of bad prime numbers.

Output

Print a single integer — the answer to the problem.

Sample test(s)
Input
5 2
4 20 34 10 10
2 5
Output
-2
Input
4 5
2 4 8 16
3 5 7 11 17
Output
10
Note

Note that the answer to the problem can be negative.

The GCD(x1, x2, ..., xk) is the maximum positive integer that divides each xi.

#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n,m;
int a[5010];
int p[100010];
int gg[5010];
int gcdnum;
map prime;
void predeal()
{
     memset(p,0,sizeof(p));
     prime.clear();
     for(int i=2;i<=100000;i++)
     {
         if(p[i]) continue;
         prime[i]=1;
         for(int j=i+i;j<=100000;j+=i) p[j]=1;
     }
}
int gcd(int a,int b)
{
   return a==0?b:gcd(b%a,a);
}

int work(int num)
{
    int sum=0;
    if(num==1) return sum;
    map::iterator it;
    for(it=prime.begin();it!=prime.end();it++)
    {
        while(num%(it->first)==0)
        {
            sum+=(it->second);
            num/=(it->first);
        }
        if(num==1) return sum;
    }
    if(num>1) return sum+1;
}

int main()
{

   //freopen("in.in","r",stdin);
     while(~scanf("%d%d",&n,&m))
     {

         predeal();
         for(int i=0;i=0;i--)
         {
            int tp=work(gg[i]);
          //  printf("tp== %d ",tp);
            if(tp


你可能感兴趣的:(数学)