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的更多相关文章

随机推荐

  1. 1038 统计同成绩学生 (20 分)C语言

    题目描述 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入描述: 输入在第1行给出不超过105的正整数N,即学生总人数.随后1行给出N名学生的百分制整数成绩,中间以空格分隔.最后1 ...

  2. Echarts大数据可视化物流航向省份流向迁徙动态图,开发全解+完美参数注释

    最近在研究Echarts的相关案例,毕竟现在大数据比较流行,比较了D3.js.superset等相关的图表插件,还是觉得echarts更简单上手些. 本文是以原生JS为基础,如果使用Vue.js的话, ...

  3. ThreadLocal解析:父线程的本地变量不能传递到子线程详解

    众所周知,ThreadLocal类是java提供线程本地变量的工具类.但父线程的本地变量却不能被子线程使用,代码如下: public static void main(String[] args) { ...

  4. Map and HashMap

    1.1.1. Map 接口 java提供了一组可以以键值对(key-value)的形式存储数据的数据结构,这种数据结构称为Map.我们可以把Map看成一个多行两列的表格,其中第一列存放key,第二列存 ...

  5. 【Java基础总结】网络编程

    网络编程 InetAddress tcp udp

  6. 数据结构与算法 Python语言实现 第一章练习

    说明:部分代码参考了Harrytsz的文章:https://blog.csdn.net/Harrytsz/article/details/86645857 巩固 R-1.1 编写一个Python函数 ...

  7. target 和 currentTarget的区别

    target是当前点击的组件,currentTarget是扑捉到事件的组件

  8. 《【面试突击】— Redis篇》--Redis Cluster及缓存使用和架构设计的常见问题

    能坚持别人不能坚持的,才能拥有别人未曾拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! <[面试突击]— Redis篇>--Redis Cluster及缓存使用和架构设计的 ...

  9. 洛谷p1137 模拟退火

    题目链接:https://www.luogu.org/problem/P1337 以x为原点,将力分解成横纵方向的力,每次退火时单独对答案的横纵坐标进行判断是否更新答案 #include<ios ...

  10. 12.方法重载overload

    方法重载:overload 重载就是在一个类中,有相同的函数名称,但形参不同的函数 方法重载的规则: 方法名称必须相同 参数列表必须不同(个数不同.或类型不同.参数排列顺序不同等) 方法的返回值类型可 ...