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. SpringBoot2 整合 Zookeeper组件,管理架构中服务协调

    本文源码:GitHub·点这里 || GitEE·点这里 一.Zookeeper基础简介 1.概念简介 Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务.从设计模式角度来 ...

  2. java线程相关基本方法

    java线程中常用的基本方法有wait,notify,notifyAll,sleep,join,yield等. 线程的生命周期一共分为五个部分,分别是:新建(New).就绪(Runnable).运行( ...

  3. spring boot学习笔记(2)

    Spring boot集成mybatis的三种方式 一.XML文件 在pom文件里面引入mybatis和数据库的依赖 在application.properties中加入数据源配置 其他和ssm配置完 ...

  4. 拥有 GitHub 开源项目的小伙伴,免费申请 JetBrains 全家桶的全流程详解

    工欲善其事,必先利其器.如果您想要学习 Java.PHP.Ruby.Python.JavaScript.Objective-C..NET 中的任何一种开发技术,国际知名且屡获殊荣的 JetBrains ...

  5. 原生javascript 基础动画函数封装(二)

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  6. win10关闭系统更新 (转载)

    一.关闭Windows10系统的自动更新服务 1:使用快捷键Win+R,打开运行 2:输入命令:services.msc,打开系统服务界面 找到Windows Update双击 将启动类型改为[禁用] ...

  7. java8 stream自定义分组求和并排序

    public static void main(String[] args) { List<GroupDetailDTO> list = new ArrayList<>(); ...

  8. typescript 第一弹

    typescript官网: http://typescriptlang.org typescript 在线运行环境: http://www.typescriptlang.org/play/index. ...

  9. 创建认证提供器 安全性与收尾工作 精通ASP-NET-MVC-5-弗瑞曼

  10. vijos 分梨子

    点击打开题目 很有(wei)趣(suo)的一道题 暴力解法也不难,枚举大小下限与甜度下限,在一个一个地试 显然 O(n^3) 炸掉 但如何将其缩短,只好从那个式子来入手了: C1⋅(ai−a0)+C2 ...