Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it.
You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw.
The only line contains an integer n (3 ≤ n ≤ 101; n is odd).
Output a crystal of size n.
3
*D* DDD *D*
5
**D** *DDD* DDDDD *DDD* **D**
7
***D*** **DDD** *DDDDD* DDDDDDD *DDDDD* **DDD** ***D***
水题:
贴个比较短的代码,注意到字符为‘D’的最外层到最中心的距离是一定的。 这里距离是|xi - x0| + |y1 - y0|, (x0, y0)是最中心坐标。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int N;
scanf("%d",&N);
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
cout << ((abs(j-N/2)+abs(i-N/2))<(N/2+1)?"D":"*");
}cout << endl;
}
}
One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:
Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
2 2 1
1
3 1 3 2
-1
2 1 2
0
题意就是 每次把这个数列的最后一个拿到第一个来。 问最少多少次可以把序列变为严格递增的。
#include <iostream>
using namespace std;
int a[200010], b[200010];
int main()
{
int i, n;
cin >> n;
for(i = 1;i <= n; i++ ) cin >> a[i];
for(i = n + 1; i < 2 * n; i++ ) a[i] = a[i - n];
b[1] = 1;
int ans = -1;
for(i = 2;i < 2 * n;i++)
{
if(a[i - 1] <= a[i] ) b[i] = b[i-1] + 1;
else b[i] = 1;
if(b[i] == n ) { ans = n + 1 - (i - n + 1); break;}
}
if(ans == n) ans = 0;
if(ans == -1) cout << "-1" << endl;
else cout << ans << endl;
return 0;
}
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains mdots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
6 1
3.500000000000
6 3
4.958333333333
2 2
1.750000000000
Consider the third test example. If you've made two tosses:
- You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
- You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
- You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
- You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
![](https://i-blog.csdnimg.cn/blog_migrate/c139655a313c7b44cdf167d764177120.png)
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
题意:
m表示给定的骰子的最大点数(比如我们平常用的是6), n是骰子的个数。
每次掷这个n个骰子,求得到的最大值的期望。
注意到最大值不大于A的概率为 A^n/m^n, 不大于A-1的概率为(A-1)^n/m^n,那么前者减去后者得到的就是最大值为A的概率。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int m, n, i;
double ans = 0, anse = 0;;
cin >> m >> n;
for(i = 1; i <= m; i++ )
{
ans += pow(i * 1.0 / m, n);
ans -= pow((i - 1) * 1.0 / m, n);
anse += ans * i;
ans = 0;
}
cout << anse << endl;
return 0;
}
Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
![](https://i-blog.csdnimg.cn/blog_migrate/46e717efa1569f152b4199daf7a0ec54.png)
A sequence of positive integers bi is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence bi which minimizes the following expression:
![](https://i-blog.csdnimg.cn/blog_migrate/ebae29064716836aa36a058a2573ede9.png)
You are given sequence ai, help Princess Twilight to find the key.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of elements of the sequences a and b. The next line contains n integersa1, a2, ..., an (1 ≤ ai ≤ 30).
Output the key — sequence bi that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them.
5 1 1 1 1 1
1 1 1 1 1
5 1 6 4 2 8
1 5 3 1 8
题意:
给定一个a数列,让你找到一个b数列满足:b中任意两个数互素,且最小。输出b数列。
状态压缩dp
dp[i][j]表示前i个数状态为j的时候的最小值,pre[i][j]表示该状态对应第i位取的数。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define ms0(X) memset((X), 0, sizeof((X)))
#define ms1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
typedef long long LL;
#define M (1 << 16)
#define inf 123456789
int i, j, k;
int n, a[101], dp[101][M], bi[60], pre[101][M];
int prime[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
void init()
{
rep(i, 1, 58)
{
rep(j, 0, 15)
{
if(prime[j] > i) break;
if(i % prime[j] == 0) bi[i] = bi[i] | (1 << j);
}
}
}
void solve()
{
//dp[i][j]: .
rep(i, 1, n)
{
reps(j, 0, M) dp[i][j] = inf;
reps(j, 0, M)
{
rep(k, 1, 58)
{
if((bi[k] & j) == 0 && dp[i-1][j] + abs(k - a[i]) < dp[i][j | bi[k]] )
{
dp[i][j | bi[k]] = dp[i-1][j] + abs(k - a[i]);
pre[i][j | bi[k]] = k;
}
}
}
}
}
void print()
{
vector<int> ans;
for(i = n, k = min_element(dp[i], dp[i] + M) - dp[i]; i >= 1; k = k & (~bi[pre[i][k]]), i--)
ans.pb(pre[i][k]);
for(i = 0; i < n; i++)
printf("%d%c", ans[n - 1 - i], i == n - 1 ? '\n' : ' ');
}
int main()
{
cin >> n;
rep(i, 1, n) cin >> a[i];
init();
solve();
print();
return 0;
}