C
Princess Celestia, is an Alicorn pony, the former co-ruler of Equestria alongside her younger sister Princess Luna. Princess Celestia raises the sun and governs Equestria, while Princess Luna raises the moon and oversees the realm of dreams.
In early days, Luna felt overshadowed and ignored by the ponies of Equestria, who slept through her beautiful night sky. These negative emotions festered and eventually led her to be consumed by the darkness and power of the mythical creature known as Nightmare Moon.
The darkness was spreading, and could be transformed into the following model:
In an n×m white 2D grid, there are several black grid points.
Every second, all white points that are 4-neighbors of at least two black points will turn black.
What is the minimum number of black points needed to turn the entire grid black in the end?
题意: 就是给你n*m的图问你最少需要多少黑点可以让这个方格全部变黑,而让一个方格变黑的条件是他的上下左右有两个以上是黑的
代码:
void solve()
{
int n, m;
cin >> n >> m;
if(n<m)
swap(n,m);
if(m == 1)
cout << n/2+1<< '\n';
else
{
int cnt1 = (n + 1) / 2;
int cnt2 = m / 2;
if (m % 2 != 0 && n % 2 == 0)
{
cout << n / 2 + m / 2 + 1 << '\n';
}
else cout << cnt1 + cnt2 << '\n';
}
}
H
Walk Alone has a graph of n nodes and m bi-directional edges. He defines a function of f(u,v)=(degu⊕degv)(degu|degv)(degu°v), where degudeg� denotes the number of edges that associated with node u, and ⊕,|,&denotes bitwise xor, bitwise or and bitwise and. He wants you to calculate (∑ni=1∑nj=if(i,j))mod998 244 3533.
Input
The first line of the input contains two integers n,m (1≤n≤106,0≤m≤106) denoting the number of nodes and edges.
Each of the following m lines contains two integers u,v (1≤u,v≤n) representing a bi-directional edge (u,v).
The graph might have duplicated edges and self-loops. Remind that a self-loop on node x will contribute 2 to degxdeg.
Output
Output a single integer denoting the answer.
Examples
input
Copy
6 6
1 3
2 3
1 4
2 5
3 6
4 6
output
Copy
30
input
Copy
6 4
1 2
3 5
2 4
3 6
output
Copy
0
思路: 一开始把每一个点出现的次数加起来然后你会发现他的的点的值是一定的所以我们只要对这些值进行操作就可以了
代码
void solve()
{
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;i ++)
{
int x,y;
cin >> x >> y;
deg[x]++;
deg[x]%=mod;
deg[y]++;
deg[y]%=mod;
}
for(int i = 1;i <= n;i ++)
{
if (!mp[deg[i]]) a.push_back(deg[i]);
mp[deg[i]]++;
}
// cout << '\n';
int ans = 0;
for(int i = 0;i < a.size();i ++)
{
// cout << a[i] << ' ';
int u = 0;
for(int j = i;j < a.size();j ++)
{
//int w = ((((a[i] ^ a[j]) % mod) * ((a[i] | a[j]) % mod)) % mod * ((a[i] & a[j]) % mod)) %mod;
// cout << w << ' ' << a[i] << ' ' << a[j] << '\n';
int w = 0;
w = (a[i]^a[j])%mod;
w = (w*(a[i]|a[j]))%mod;
w = (w*(a[i]&a[j]))%mod;
w = w * mp[a[j]] % mod;
u = (u + w + mod) % mod;
}
ans = ((ans + (u * mp[a[i]]) % mod) % mod + mod) % mod;
// cout << u << ' ' << ans << ' ' << a[i] << '\n';
}
// cout << '\n';
cout << ans << '\n';
}
J
Applejack is a female Earth pony. She lives and works at Sweet Apple Acres with her grandmother Granny Smith, her older brother Big McIntosh, her younger sister Apple Bloom, and her dog Winona. She represents the element of honesty.
Now Applejack is going to cultivate a row of wasteland, and it could be transformed into the following model:
There are n cells in a row, each containing an integer ai. Applejack start by cultivating cell number 1 and have 00 resources just before the end of time unit 1.
Later, assuming Applejack have cultivated cells [1,x], at the beginning of each unit time, she can cultivate cell x+1, and at the end of each unit time, she obtain resources equal to the sum of integers on all cells she have cultivated.
Applejack need to ensure that she always have non-negative resources(even after cultivating all cells). What is the minimum time needed to occupy all cells, or is it impossible?
Input
The first line contains an integer n (2≤n≤105) which represents the length of the row of cells.
The second line contains n integers ai (−108≤ai≤108), representing the coefficients on the cells.
Output
Output a single integer representing the minimum amount of time needed to occupy all cells, if a solution exists.
Otherwise, output −1.
思路: 先用前缀和把每一个点对应的值的变化求出来,然后从前往后每一位的变化的最大值记录下来,后面如果需要等待则在这个点的最大值进行计算就可以了
代码
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) s[i] = s[i - 1] + a[i];
if (s[n] < 0) {cout << -1 << '\n';return;}
for(int i = 1;i <= n;i ++)
{
if (s[i] < 0) {cout << -1 << '\n';return;}
if(s[i] > 0)
break;
}
for (int i = 1; i <= n; i ++)
{
p[i] = max(p[i - 1], s[i]);
}
int ans = 0, res = 0;
for (int i = 1; i <= n; i ++)
{
if (s[i]+ res < 0)
{
int w = ((abs(s[i]) - res) + p[i] - 1) / p[i];
res += w*p[i];//- res;
res += s[i];
//cout << i << ' '<< w << ' ' << res << '\n';
ans += w;
ans++;
}
else
{
ans ++;
res += s[i];
// if(a[i] < 0)
// res += a[i];
}
//cout << res << '\n';
}
cout << ans << '\n';
}
K
There are n+1 people playing a game with dice. Every person has a dice of m faces, each of which is equal-probability. Don't spend time wondering how this kind of dice can exist–You can image a generator which can generate numbers among [1,m] with equal-probability.
At the beginning, each person throws his own dice, and the person with the smallest number of points loses. If more than one person throws the smallest number of dice at the same time, those who have thrown the smallest number of dice continue to throw dice until finally one person loses.
Walk Alone doesn't like randomness. He uses magic to fix the points the first person throws every time. That is, the first person will always throw x points when the first person throws his dice, where x is pre-chosen by Walk Alone, while other n people still throw their dices randomly.
Now Walk Alone wants to know for every x∈[1,m], the probability the first person will lose.
To be more precise, you need to output the probability modulo 998 244 353998 244 353. It can be proven that the probability can be presented as a fraction pq. You need to output p⋅q998 244 351.
Input
The only line of input contains two integers n,m(1≤n≤105) denoting the number of people and the number of faces a dice is.
Output
Output m integers in a line, the i-th number denoting the probability modulo 998 244 353998 244 353 when x=i.
这个直接推公式
代码
void solve()
{
int n,m;
cin >> n >> m;
cout << "1 ";
rep(i,2,m-1)
{
cout << qmi((m-i) * qmi(m-1,mod - 2,mod) % mod, n, mod) << " ";
}
cout << 0;
}
M
Once upon a time, there was an onsite programming contest, which had attracted many teams to participate in. However, different teams had a different billing standard. In general, there were three types of teams:
- Teams of Type A could participate in the contest for free.
- Teams of Type B had to pay 1000 dollars for the expenses for board and lodging.
- Teams of Type C had to pay 1000 dollars for the expenses for board and lodging and 1500 dollars for entry fee.
Finally the contest was held successfully with x teams in total participating in, and the host had earned y dollars. However, the details about the number of teams of each type had vanished. Now Walk Alone wants to restore the details using x and y.
Input
The only line of input contains two integers x and y (0≤x≤106), denoting the number of teams in total and the total amount of money the host had earned.
Output
If there's a way to construct the number three types of teams satisfying the condition above, output three integers A,B,C to denote the number of teams of type A, B and C. You need to guarantee that 0≤A,B,C. If there are multiple ways, output any of them.
If there's no answer satisfying the condition above, output −1 instead.
代码
void solve()
{
int x, y;
cin >> x >> y;
if (y % 1000 == 0)
{
int b = y / 1000;
int c = b / 5 * 2;
b %= 5;
if (x < b + c) {cout << -1 << '\n';return;}
cout << x - b - c << ' ' << b << ' ' << c << '\n';
}
else
{
int res = y - 2500, c = 1;
if (res % 1000 != 0) {cout << -1 << '\n';return;}
int b = res / 1000;
c += b / 5 * 2;
b %= 5;
if (x < b + c) {cout << -1 << '\n';return;}
cout << x - b - c << ' ' << b << ' ' << c << '\n';
}
}