A. Mashmokh and Lights
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Mashmokh works in a factory. At the end of each day he must turn off all of the lights.
The lights on the factory are indexed from 1 to n. There are n buttons in Mashmokh's room indexed from 1 to n as well. If Mashmokh pushes button with index i, then each light with index not less than i that is still turned on turns off.
Mashmokh is not very clever. So instead of pushing the first button he pushes some of the buttons randomly each night. He pushed mdistinct buttons b1, b2, ..., bm (the buttons were pushed consecutively in the given order) this night. Now he wants to know for each light the index of the button that turned this light off. Please note that the index of button bi is actually bi, not i.
Please, help Mashmokh, print these indices.
The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 100), the number of the factory lights and the pushed buttons respectively. The next line contains m distinct space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n).
It is guaranteed that all lights will be turned off after pushing all buttons.
Output n space-separated integers where the i-th number is index of the button that turns the i-th light off.
题意 : 如果按下一个开关,所有编号比这个开关的编号大的灯都会被关掉,让你输出n盏灯分别是被那个开关关上的。
思路 : 标记即可
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int a[110];
int main()
{
int n,m,b;
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
while(m--)
{
scanf("%d",&b);
for(int i = b;i <= n; i++)
{
if(!a[i])
a[i]=b;
}
}
for(int i = 1; i <= n; i++)
i!= n ? printf("%d ",a[i]) : printf("%d\n",a[i]);
return 0;
}
B. Mashmokh and Tokens
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives backw tokens then he'll get dollars.
Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x1, x2, ..., xn. Number xi is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save.
The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input containsn space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109).
Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day.
5 1 4
12 6 11 9 1
0 2 3 1 1
3 1 2
1 2 3
1 0 1
1 1 1
1
题意:如果一个人有w张票,那他会得到w*a/b取整个钱,现在让你在保证得到的钱数不变的情况下能够省得最多的票是多少。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
long long a,b,w;
int n;
scanf("%d%I64d%I64d",&n,&a,&b);
for(int i = 0; i < n ; i++)
{
scanf("%I64d",&w);
long long t = a*w/b;
long long y;
if(t*b%a==0)
y=t*b/a;
else
y=t*b/a+1;
printf("%I64d ",w-y);
}
cout<<endl;
return 0;
}
C. Mashmokh and Numbers
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.
Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way.
Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.
The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 105; 0 ≤ k ≤ 108).
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
5 2
1 2 3 4 5
5 3
2 4 3 7 1
7 2
-1
gcd(x, y) is greatest common divisor of x and y.
题意: 给你n和k,让你找出n个完全不一样的数,要求gcd(a1,a2)+gcd(a3,a4)+.....+gcd(an-1,an) = k。如果n是奇数则最后那个数就不用算了就是要满足gcd(a1,a2)+....+gcd(an-2,an-1) = k。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
int n,k;
int i,co;
scanf("%d%d",&n,&k);
if(k < n/2 || (n<2&&k!=0) ) //不可能出现的情况
{
printf("-1\n") ;
return 0;
}
else if(k == n/2) //剪枝
{
for(int i = 1 ; i < n ; i++)
printf("%d ",i) ;
printf("%d\n",n) ;
}
else
{
int x = k-n/2+1;
printf("%d %d ",x,x*2) ;
for(int i = x*2+1 ;i <= x*2+n-2; i++)
printf("%d ",i);
cout<<endl;
}
return 0;
}