C - Cats
Little E has n cathouses in a line. For each integer height between 1 and 20, there are sufficiently many cats with that height. He should choose n cats to place in these cathouses. Every cathouse should contain exactly one cat. However, these cats have a special habit. For every two different cats with the same height, they can’t bear to live adjacently, and they can’t bear the height of the shortest cat living in the cathouse between them is greater than or equal to theirs.
It’s too hard for Little E to find a scheme to make all the cats living in cathouses satisfied. Can you help him?
Input
The first line contains an integer n (1≤n≤105) — the number of cathouses.
Output
Output n integers in a line. For the i-th integer, output the height of the cat living in the i-th cathouse. Any scheme which can make all the cats living in cathouses satisfied is acceptable.
Examples
Input
1
Output
1
Input
3
Output
1 2 3
构造一种合理的解,我们最少可以把1放进去,然后可以把2插到有空的地方,然后再依次把3插入到有空的地方即可,没增大一个数,就能把那个数插到有空的地方,然后找规律,从大到小每个数的位置都是可以找到规律的
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 25;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
int pre[N];
int a[100010];
signed main(){
pre[1] = 1;
for (int i = 2; i <= 20; i ++){
pre[i] = pre[i - 1] * 2;
}
int n;
scanf("%lld", &n);
int k = 1;
int sum = 0;
while(sum < n){
sum += pre[k];
k ++;
}
k --;
int idx = 0;
while(k > 0){
for (int i = pow(2, idx ++); i <= n; i += pow(2, idx)){
a[i] = k;
}
k --;
}
for (int i = 1; i <= n; i ++){
printf("%lld ", a[i]);
}
cout << endl;
return 0;
}
J - Just Multiplicative Inverse
Just Multiplicative Inverse
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes
Yukikaze is learning number theory. She found a mysterious function to compute multiplicative inverse of
integers modulo a prime p. The pseudocode of the function is as follows:
1: function F(x, p)
2: if x ≤ 1 then
3: return 1
4: else
5: return −bp/xc · F(p mod x, p) mod p
6: end if
7: end function
She wants to know the expected number of calls if she calls this function with a random integer uniformly
distributed in the range [1, p − 1].
Input
The first line of the input contains a single integer T (1 ≤ T ≤ 100), denoting the number of test cases.
Each of the next T lines contains a single integer p (2 ≤ p ≤ 106
, p is a prime), denoting the parameter
of the function described above.
Output
For each test case, output the answer in a single line.
Your answer is considered correct if its absolute or relative error does not exceed 10−6
.
Formally, let your answer be a, and the jury’s answer be b. Your answer will be accepted if |a−b|
max{1,|b|} ≤ 10−6
.
Example
standard input standard output
5
2
3
5
7
999983
1.0000000000
1.5000000000
2.0000000000
2.1666666667
15.9864347558
Note
For the 4-th test case in example, we have:
F(1, 7)
F(2, 7) → F(1, 7)
F(3, 7) → F(1, 7)
F(4, 7) → F(3, 7) → F(1, 7)
F(5, 7) → F(2, 7) → F(1, 7)
F(6, 7) → F(1, 7)
So the answer is (1 + 2 + 2 + 3 + 3 + 2)/6 = 2.16666666 · ·
递归即可,可以增加记忆化,来使程序加快,时间复杂度玄学
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
using namespace std;
#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
inline int read(int out = 0)
{
char c;
while((c=getchar()) < 48 || c > 57);
while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
return out;
}
const int N = 1e6 + 7;
const int M = 4 * N;
const int mod = 1e4 + 7;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);
double st[N];
int p;
double f(int x){
if (x <= 1) return 1;
else if (st[x]) return st[x];
else {
return st[x] = f(p % x) + 1;
}
}
signed main(){
int T;
scanf("%lld", &T);
while(T --){
memset(st, 0, sizeof st);
scanf("%lld", &p);
double sum = 0;
for (int i = 1; i < p; i ++){
sum += f(i);
}
double ans = (sum) / (double)(p - 1);
//
printf("%.10lf\n", ans);
}
return 0;
}