A:honoka和格点三角形
题意: 定义满足以下三个条件的三角形是“好三角形”:
1.三角形的三个顶点均为格点,即横坐标和纵坐标均为整数。
2.三角形的面积为1。
3.三角形至少有一条边和x轴或y轴平行。
思路:
面积为1,那就是底为1高为2,底为2高为1。注意底为1高为2时不能有出现。
以横着为底边举例:
底2高1:下部横坐标可以取m-2种,高上部可以取m个,然后除了第一行和最后一行只能取一种其他都是上下2种,
(
m
−
2
)
∗
m
∗
(
n
−
1
)
∗
2
(m - 2)*m*(n - 1)*2
(m−2)∗m∗(n−1)∗2
底1高2:下部横坐标可以取m-1种,高上部可以取m-2个,然后除了第一行第二行和最后一行倒数第二行只能取一种其他都是上下2种,
(
m
−
1
)
∗
(
m
−
2
)
∗
(
n
−
2
)
∗
2
(m - 1)*(m - 2)* (n - 2) * 2
(m−1)∗(m−2)∗(n−2)∗2
代码:
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int main(){
ll m,n;scanf("%lld %lld",&m,&n);
printf("%lld\n",((m - 2) % mod * m % mod * (n - 1) % mod * 2 % mod + (m - 1) * (m - 2) % mod* (n - 2) % mod* 2 % mod + (n - 2) % mod * n % mod * (m - 1) % mod * 2 % mod + (n - 1) * (n - 2) % mod * (m - 2) % mod* 2 % mod)%mod);
}
B:kotori和bangdream
签到题不多赘述了,按题意模拟即可:
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int main(){
double n;double x,a,b;
scanf("%lf",&n);
scanf("%lf%lf%lf",&x,&a,&b);
printf("%.2f",n*(x/100)*a+n*(1 - x/100)*b);
}
C:umi和弓道
题意:给你1个原始坐标点,n个目标点,让你在坐标轴上找出最短的一端长度,使得有n - k个点经过这段长度。
思路:首先和他同个象限得不可能被拦截,这些我们不要,然后通过两点求出与x轴与y轴交点存到vector里,然后排序以后求一下连续 得n-k个点需要多长,ans取min就行了。
#include<bits/stdc++.h>
typedef long long int ll;
const int maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
using namespace std;
vector<double>x,y;
int main(){
double x0,y0;
scanf("%lf %lf",&x0,&y0);
int n,k;scanf("%d %d",&n,&k);
for(int i = 1;i <= n;i++){
double a,b;scanf("%lf %lf",&a,&b);
if(a * x0 < 0) y.push_back(y0 - (x0 * (y0 - b)) / (x0 - a));
if(b * y0 < 0) x.push_back(x0 - (y0 * (x0 - a)) / (y0 - b));
}
if(n - x.size() > k && n - y.size() > k) { puts("-1");return 0;}
sort(x.begin(),x.end());sort(y.begin(),y.end());
k = n - k;double ans = 1e18;
for(int i = 0;i + k - 1 < x.size();i++) ans = min(ans,x[i + k - 1] - x[i]);
for(int i = 0;i + k - 1 < y.size();i++) ans = min(ans,y[i + k - 1] - y[i]);
printf("%.9f",ans);
}
D:hanayo和米饭
签到题,sort一下找就好了。
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int n;
int a[maxn];
int main(){
scanf("%d",&n);
for(int i = 1;i < n;i++) scanf("%d",&a[i]);
sort(a + 1,a + n);
for(int i = 1;i <= n;i++){
if(a[i] != i){
printf("%d\n",i);
break;
}
}
}
E:rin和快速迭代
题意:定义
f
(
x
)
f(x)
f(x)为
x
x
x的的因子数,问需要迭代
f
(
x
)
f(x)
f(x)几次才能使
f
(
x
)
=
2
f(x) = 2
f(x)=2。
思路:
f
(
x
)
f(x)
f(x)只须取一次就会降到50以下很小,因此我们可以思考判断素数的方式,只需判断到sqrt(n)即可。
代码:
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
ll getnum(ll x){
ll res = 0;
ll i;
for(i = 1;i * i <= x;i++){
if(x % i == 0) res++;
}
if((i - 1) * (i - 1) == x) return res * 2 - 1;
else return res * 2;
}
int main(){
ll n;scanf("%lld",&n);
int ans = 0;
while(1){
n = getnum(n);
if(n != 2) ans++;
else break;
}
printf("%d\n",ans + 1);
}
F:maki和tree
题意:给你一颗树n个点,告诉你每个点的颜色,由黑和白两种。然后问你只经过黑点一次的简单路径有几种。
思路:因为至今过黑点一次,那我们把每个黑点抓出来,把两个黑点之间的白点用并查集并成一个集合用sz数组存大小,然后通过容斥原理既可以推出每个黑点相邻的白色集合的贡献,累加每个黑点的贡献即可,复杂度
o
(
n
l
o
g
n
)
o(nlogn)
o(nlogn)。
代码:
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
vector<int>mp[maxn];
int has[maxn],pre[maxn];
ll sz[maxn];
char s[maxn];
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
int find(int x){
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
void Union(int x,int y){
int fx = find(x),fy = find(y);
if(fx != fy)pre[fx] = fy,sz[fy] += sz[fx];
}
int main(){
int n;n = read();
for(int i = 1;i <= n;i++) sz[i] = 1,pre[i] = i;
scanf("%s",s + 1);
for(int i = 1;i <= n;i++) if(s[i] == 'B') has[i] = 1;
for(int i = 1;i < n;i++){
int u,v;u = read(),v = read();mp[u].push_back(v);mp[v].push_back(u);
if(!has[u] && !has[v]) Union(u,v);
}
ll ans = 0;
for(int i = 1;i <= n;i++){
if(!has[i]) continue;
ll res = 0;
for(auto it : mp[i]){
if(has[it]) continue;
ans += res * sz[find(it)];res += sz[find(it)];
}
ans += res;
}
printf("%lld\n",ans);
}
G:eli和字符串
题意:给你一个字符串,问你这个子串包含至少k个相同的字母的最小字串长度是多少。
思路:用26个vector存每个字母出现的位置,然后去遍历每个字母,长度即为pos[i + k - 1] - pos[i]+1,取min即可。
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int n,k;
vector<int>v[30];
char s[maxn];
int main(){
scanf("%d %d",&n,&k);
scanf("%s",s);
for(int i = 0;i < strlen(s);i++){
v[s[i] - 'a'].push_back(i);
}
int ans = inf;
for(int i = 0;i < 26;i++){
if(v[i].size() < k) continue;
for(int j = 0;j < v[i].size() - k + 1;j++){
ans = min(ans,v[i][j + k - 1] - v[i][j] + 1);
}
}
if(ans == inf) ans = -1;
printf("%d\n",ans);
}
H:nozomi和字符串
题意:给你一个01字符串,你有k次机会把0变成1或者把1变成0,问你最长的全1或全0的字串的长度是多少。
思路:我变得k次一定是连续得0或者连续得1,因此我把0和1得位置存下来,然后思路和上题类似,注意处理边界,当位置为0时是在字符串最左边,当位置为最后一个1或者0时,位置是在字符串最右边,取max即可。
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
vector<int>v[2];
int main(){
int n,k;scanf("%d %d",&n,&k);
string s;cin >> s;
for(int i = 0;i < s.length();i++){
v[s[i] - '0'].push_back(i);
}
int ans = 0;
if(v[0].size() <= k || v[1].size() <= k) printf("%d\n",s.length());
else {
for(int i = 0;i <= 1;i++){
int res = 0;
for(int j = 0;j < v[i].size() - k + 1;j++){
int pos1;int pos2;
if(j == 0) pos1 = 0;else pos1 = v[i][j - 1];
if(j + k == v[i].size()) pos2 = s.length();else pos2 = v[i][j + k];
res = max(res,pos2 - pos1 - 1);
}
ans = max(ans,res);
}
printf("%d\n",ans);
}
}
I:nico和niconiconi
一道不知道做了多少次得傻逼线性dp,就是写的时候字符串有点长;
#include<bits/stdc++.h>
#define accept return 0
#define mes(a, b) memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 3e5 + 5;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;
int n;
ll dp[maxn];
char s[maxn];
int main(){
int n;scanf("%d",&n);
ll a,b,c;scanf("%lld %lld %lld",&a,&b,&c);
scanf("%s",s + 1);
for(int i = 1;i <= n;i++){
if(i >= 4){
if(s[i] == 'o' && s[i - 1] == 'c' && s[i - 2] == 'i' && s[i - 3] == 'n') dp[i] = max(dp[i - 4] + a,dp[i]);
}
if(i >= 6){
if(s[i - 5] == 'n' && s[i - 4] == 'i' && s[i - 3] == 'c' && s[i - 2] == 'o' && s[i - 1] == 'n' && s[i] == 'i') dp[i] = max(dp[i - 6] + b,dp[i]);
}
if(i >= 10){
if(s[i - 9] == 'n' && s[i - 8] == 'i' &&s[i - 7] == 'c' &&s[i - 6] == 'o' &&s[i - 5] == 'n' && s[i - 4] == 'i' && s[i - 3] == 'c' && s[i - 2] == 'o' && s[i - 1] == 'n' && s[i] == 'i') dp[i] = max(dp[i - 10] + c,dp[i]);
}
dp[i] = max(dp[i -1],dp[i]);
}
printf("%lld\n",dp[n]);
}
J先留坑,晚点补。留下代码:
#include<bits/stdc++.h>
typedef long long int ll;
const int maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
using namespace std;
struct Mat{
ll mat[4][4];
Mat(){
memset(mat,0,sizeof(mat));
}
Mat operator * (const Mat o)const{
Mat ans;
for(int i = 0;i < 4;i++){
for(int j = 0;j < 4;j++){
for(int k = 0;k < 4;k++){
ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * o.mat[k][j]) % (mod - 1);
}
}
}
return ans;
}
}ans,res;
Mat matpow(Mat a,ll n){
Mat ans;
for(int i = 0;i < 4;i++) ans.mat[i][i] = 1;
while(n){
if(n & 1) ans = ans * a;
a = a * a;
n >>= 1;
}
return ans;
}
ll qpow(ll a,ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void init1(){
memset(ans.mat,0,sizeof ans);memset(res.mat,0,sizeof res);
res.mat[0][0] = 0;res.mat[0][1] = 1;
res.mat[1][0] = 1;res.mat[1][1] = 1;
}
void init2(){
memset(ans.mat,0,sizeof ans);memset(res.mat,0,sizeof res);
res.mat[0][0] = 0;res.mat[0][1] = 1;res.mat[0][2] = 0;
res.mat[1][0] = 1;res.mat[1][1] = 1;res.mat[1][2] = 1;
res.mat[2][0] = 0;res.mat[2][1] = 0;res.mat[2][2] = 1;
}
int main(){
ll n,x,y,a,b;scanf("%lld %lld %lld %lld %lld",&n,&x,&y,&a,&b);
if(n == 1) { printf("%lld\n",x % mod);return 0;}
else if(n == 2) {printf("%lld\n",y % mod);return 0;}
a = qpow(a%mod,b);x %= mod;y %= mod;
init1();
ans.mat[0][0] = 1;ans.mat[1][0] = 0;
ans = ans * matpow(res,n - 1);
ll ans1 = ans.mat[0][0];
init1();
ans.mat[0][0] = 0;ans.mat[1][0] = 1;
ans = matpow(res,n - 1) * ans;ll ans2 = ans.mat[0][0];
init2();
ans.mat[0][0] = 0;ans.mat[1][0] = 0;ans.mat[2][0] = 1;
ans = matpow(res,n - 1) * ans;ll ans3 = ans.mat[0][0];
if(x % mod == 0 || y % mod == 0 || a % mod == 0) puts("0");
else {
printf("%lld\n",qpow(x,ans1) * qpow(y,ans2) % mod * qpow(a,ans3) % mod);
}
}