Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integerj, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds,min(ri, rj) < max(li, lj).
In a single line print a single integer — the answer to the problem.
2 3 1 2 3 4
2
3 7 1 2 3 3 4 7
0
题目大意:
给出N个区间,我们每一次操作可以拓展一个区间让其区间长度+1.问最少操作多少次能够使得区间长度和为k的倍数。
思路:
暴力搞就行。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int sum=0;
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
sum+=y-x+1;
}
printf("%d\n",(k-sum%k)%k);
}
}
Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.
In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.
The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≤ aij ≤ 104).
In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).
2 2 2 2 4 6 8
4
1 2 7 6 7
-1
题目大意:
给出一个N*M的矩阵,我们每一次操作可以将一个格子上的数+d或者是-d.问最少操作次数,使得所有数字都相等。
思路:
观察到数据范围不大,我们直接O(1e4)枚举答案然后O(N*M)去维护每个位子变成答案的花费,求和后维护最小值即可。
Ac代码:
#include <bits/stdc++.h>
using namespace std;
int a[150][150];
int main()
{
int n,m,d;
while(~scanf("%d%d%d",&n,&m,&d))
{
int flag=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&a[i][j]);
}
}
int ans=0x3f3f3f3f;
for(int z=0; z<=10000; z++)
{
int flag=0;
int sum=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(abs(a[i][j]-z)%d!=0)flag=1;
sum+=abs(a[i][j]-z)/d;
}
}
if(flag==0)
ans=min(ans,sum);
}
if(ans==0x3f3f3f3f)printf("-1\n");
else
printf("%d\n",ans);
}
}
Little penguin Polo adores strings. But most of all he adores strings of length n.
One day he wanted to find a string that meets the following conditions:
- The string consists of n lowercase English letters (that is, the string's length equals n), exactly k of these letters are distinct.
- No two neighbouring letters of a string coincide; that is, if we represent a string as s = s1s2... sn, then the following inequality holds,si ≠ si + 1(1 ≤ i < n).
- Among all strings that meet points 1 and 2, the required string is lexicographically smallest.
Help him find such string or state that such string doesn't exist.
String x = x1x2... xp is lexicographically less than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there is such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The characters of the strings are compared by their ASCII codes.
A single line contains two positive integers n and k (1 ≤ n ≤ 106, 1 ≤ k ≤ 26) — the string's length and the number of distinct letters.
In a single line print the required string. If there isn't such string, print "-1" (without the quotes).
7 4
ababacd
4 7
-1
题目大意:
让你构造出一个长度为N的字符串,使得其正好包含K个字母,使得字典序最小。
思路:
贪心即可,我们ab重复放置,然后最后边放置其他字母即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char ans[1500000];
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
if(k>n)
{
printf("-1\n");
continue;
}
if(k==1)
{
if(n==1)printf("a\n");
else printf("-1\n");
}
else
{
int now=k;
int i;
for(i=n;i>=1;i--)
{
if(k==2)break;
ans[i]='a'+k-1;
k--;
}
k=1;
for(int z=1;z<=i;z++)
{
ans[z]='a'+k-1;
k=3-k;
}
for(int i=1;i<=n;i++)printf("%c",ans[i]);
printf("\n");
}
}
}
Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).
Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a house number x. Then he goes to the house whose number is written on the plaque of house x (that is, to house px), then he goes to the house whose number is written on the plaque of house px (that is, to house ppx), and so on.
We know that:
- When the penguin starts walking from any house indexed from 1 to k, inclusive, he can walk to house number 1.
- When the penguin starts walking from any house indexed from k + 1 to n, inclusive, he definitely cannot walk to house number 1.
- When the penguin starts walking from house number 1, he can get back to house number 1 after some non-zero number of walks from a house to a house.
You need to find the number of ways you may write the numbers on the houses' plaques so as to fulfill the three above described conditions. Print the remainder after dividing this number by 1000000007 (109 + 7).
The single line contains two space-separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ min(8, n)) — the number of the houses and the number k from the statement.
In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
5 2
54
7 4
1728
题目大意:
N个位子,我们每个位子上有一个数pi,表示我们如果现在在位子i,那么下一个位子就只能去pi.
我们要构造一个序列,使得我们从1~K这些位子出发的话,都能够回到1这个位子,从K+1~N这些位子出发的话,都不能回到1这个位子。
给出N和K,问有多少种构造方式。
思路:
①我们先考虑K+1~N这些位子出发,既然我们从1~K出发都一定能够回到1,所以我们K+1~N这些位子上,只要单纯的包含从K+1~N这些数字即可,贡献方案数为:(n-k)^(n-k);
②然后考虑1~N这些位子,我们显然需要让其形成一个环 ,而且既然从2~N出发都能回到1,那么只能有1这个位子自环。考虑到K并不大,最大为8.我们本地暴力打表求出前K个位子的方案数即可。
③那么Ans=(n-k)^(n-k)*ans[k];
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
/*
int a[10];
int vis[10];
int n,output;
void Dfs(int u)
{
if(u==n+1)
{
for(int i=1;i<=n;i++)
{
int flag=0;
int now=i;
memset(vis,0,sizeof(vis));
while(vis[a[now]]==0)
{
vis[a[now]]=1;
now=a[now];
if(now==1)flag=1;
}
if(flag==0)return ;
}
output++;
return ;
}
for(int i=1;i<=n;i++)
{
a[u]=i;
Dfs(u+1);
}
}
int main()
{
while(~scanf("%d",&n))
{
output=0;
Dfs(1);
printf("%d\n",output);
}
}
*/
int ans[10]={0,1,2,9,64,625,7776,117649,2097152};
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
ll output=ans[k];
for(int i=1;i<=n-k;i++)
{
output*=(n-k);
output%=1000000007;
}
printf("%I64d\n",output);
}
}
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.
For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number .
Expression means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal — as "xor".
Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
The single line contains a positive integer n (1 ≤ n ≤ 106).
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.
If there are several suitable permutations, you are allowed to print any of them.
4
20 0 2 1 4 3
题目大意:
思路:
暴力打表找规律。。。。。。。。。我们发现位子i放置的数字如果是x的话,那么位子x放置的数字也是i,是一一对应的。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll __int64
int vis[1050000];
int ans[1050000];
int main()
{
int n;
while(~scanf("%d",&n))
{
ll output=0;
memset(vis,0,sizeof(vis));
for(int i=n;i>=1;i--)
{
if(vis[i]==0)
{
int len=(int)(log2(i));
int x=(1<<(len+1))-1-i;
vis[i]=1;
vis[x]=1;
ans[i]=x;
ans[x]=i;
output+=(i|x)*2;
}
}
printf("%I64d\n",output);
for(int i=0;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
}