Codeforces_793
A.找最小的数,看每个数跟它的差是否被k整除。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,cnt[] = {},ans[],two[]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
while(n--)
{
int x;
cin >> x;
for(int i = ;i*i <= x;i++)
{
if(x%i == )
{
cnt[i]++;
if(i*i != x) cnt[x/i]++;
}
}
}
two[] = ;
for(int i = ;i <= ;i++) two[i] = two[i-]*%MOD;
for(int i = ;i <= ;i++) ans[i] = two[cnt[i]]-;
for(int i = ;i >= ;i--)
{
for(int j = i*;j <= ;j += i) ans[i] = (ans[i]-ans[j]+MOD)%MOD;
}
cout << ans[] << endl;
return ;
}
B.直接dfs记录方向和转弯次数,不做其他剪枝就能过。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,m,vis[][][] = {},x1,Y1,flag = ;
int dx[] = {-,,,};
int dy[] = {,-,,};
string a[]; void dfs(int x,int y,int d,int cnt)
{
if(cnt > ) return;
if(flag) return;
if(a[x][y] == 'T') flag = ;
if(d != -) vis[x][y][d] = ;
for(int i = ;i < ;i++)
{
if((i+)% == d) continue;
int xx = x+dx[i],yy = y+dy[i];
if(xx < || xx > n || yy < || yy > m || a[xx][yy] == '*') continue;
if(vis[xx][yy][i]) continue;
dfs(xx,yy,i,cnt+(i != d));
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = ;i <= n;i++)
{
cin >> a[i];
a[i] = " "+a[i];
}
for(int i = ;i <= n;i++)
{
for(int j = ;j <= m;j++)
{
if(a[i][j] == 'S') x1 = i,Y1 = j;
}
}
dfs(x1,Y1,-,-);
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}
C.遍历每只老鼠,一次次缩小时间区间,最后考虑可能性,注意速率为0和边缘情况。
#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std; int n,flag = ;
double ansl = ,ansr = 1e9; void f(int x,int v,int l,int r)
{
if(v == && (x <= l || x >= r))
{
flag = ;
return;
}
double t = 1.0*(l-x)/v,tt = 1.0*(r-x)/v;
ansl = max(ansl,min(t,tt));
ansr = min(ansr,max(t,tt));
} int main()
{
ios::sync_with_stdio(false);
cin >> n;
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2; for(int i = ;i <= n;i++)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
f(a,c,x1,x2);
f(b,d,y1,y2);
}
if(flag == && ansl < ansr) cout << fixed << setprecision() << ansl << endl;
else cout << - << endl;
return ;
}
D.暴力每一个起点,每次移动,更新区间,记忆化一下,dp[i][j][k][l]表示当前在i,可行区间(j,k),已经经过l个点。
#include<bits/stdc++.h>
using namespace std; int n,m,k,g[][],dp[][][][]; int dfs(int now,int l,int r,int cnt)
{
int &ans = dp[now][l][r][cnt];
if(ans != -) return ans;
if(cnt == k)
{
ans = ;
return ;
}
ans = 1e9;
for(int i = l+;i < r;i++)
{
if(g[now][i] == 1e9) continue;
int x = l,y = r;
if(now < i) x = now;
else y = now;
ans = min(ans,g[now][i]+dfs(i,x,y,cnt+));
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> k >> m;
for(int i = ;i <= n;i++)
{
for(int j = ;j <= n;j++) g[i][j] = 1e9;
}
memset(dp,-,sizeof(dp));
for(int i = ;i <= m;i++)
{
int x,y,z;
cin >> x >> y >> z;
g[x][y] = min(g[x][y],z);
}
int ans = 1e9;
for(int i = ;i <= n;i++) ans = min(ans,dfs(i,,n+,));
if(ans == 1e9) cout << - << endl;
else cout << ans << endl;
return ;
}
Codeforces_793的更多相关文章
随机推荐
- 天梯 L2 这是二叉搜索树吗?
L2-004 这是二叉搜索树吗? (25 分) 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的 ...
- 《C++Primer》第五版习题解答--第四章【学习笔记】
[C++Primer]第五版习题解答--第四章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/11 第四章:表达式 练习4. ...
- vue设置选中时的样式名称
第一种方式:在router中全局设置 export default new Router({ mode:'history', linkActiveClass:'index', routes: [ { ...
- 基于C#的机器学习--垃圾邮件过滤
在这一章,我们将建立一个垃圾邮件过滤分类模型.我们将使用一个包含垃圾邮件和非垃圾邮件的原始电子邮件数据集,并使用它来训练我们的ML模型.我们将开始遵循上一章讨论的开发ML模型的步骤.这将帮助我们理解工 ...
- 字符串转hash进阶版
#include<bits/stdc++.h> using namespace std; ,mod=; vector<unsigned> H[mod]; void Add(un ...
- 【C_Language】---C语言const用法总结
C语言关键字const相信对于不少C语言新手是既陌生又熟悉的,好像经常见,但是却不知道为何用,怎么用?学习至此,总结一下const的用法,使用程序来帮助你理解该关键字,希望能帮到像我一样的新手. 我看 ...
- SpringSecurity 自定义表单登录
SpringSecurity 自定义表单登录 本篇主要讲解 在SpringSecurity中 如何 自定义表单登录 , SpringSecurity默认提供了一个表单登录,但是实际项目里肯定无法使用的 ...
- [题解][Codeforces]Good Bye 2019 简要题解
构造题好评,虽然这把崩了 原题解 A 题意 二人游戏,一个人有 \(k_1\) 张牌,另一个人 \(k_2\) 张,满足 \(2\le k_1+k_2=n\le 100\),每张牌上有一个数,保证所有 ...
- es8对object快速遍历的方法
let grade = { 'lilei' : 96, 'han' : 99 } //遍历keys console.log(Object.keys(grade)) console.log(Object ...
- ValidationAttribute特性的截图