Codeforces Round #506 (Div. 3) D-F
Codeforces Round #506 (Div. 3) (中等难度)
自己的做题速度大概只尝试了D题,不过TLE
D. Concatenated Multiples
题意
- 数组a[],长度n,给一个数k,求满足条件的(i,j)(i!=j) a[i],a[j]连起来就可以整除k
- 连起来的意思是 20,5连起来时205; 5,20连起来时520
- n<=2*1e5,k<=1e9,ai<=1e9
- 愚蠢的思路是像我一样遍历(i,j)可能性,然后TLE,因为这是O(n^2)
- 可以先思考一简单问题如何不是连起来而是a[i]+a[j]如何计算,很简单将a[]%k进行计数,然后计数(k-a[i])%k,这时查找只需O(logn)
- 这里a[i]*(10^k),k实际上只有10中可能所以也可以存起来
- 时间复杂度O(nlogn)
代码
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int a[200005],le[200005];
map<int,int> dp[15];
int main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
int tmp=a[i];
while(tmp){
le[i]++;
tmp/=10;
}
dp[le[i]][a[i]%k]++;//dp[len][res] len是1...10 res [0...k-1]
//res由于是map存储大概是O(nlogn)
}
ll ans=0;
for(int i=1;i<=n;i++){//个数遍历所有a[i]然后找到统计match:a[i]*(10^le[j])+a[j]的j的个数
dp[le[i]][a[i]%k]--;//是指统计时刨除自身
ll mul=1,tmp=0;
for(int j=1;j<=10;j++){
mul*=10;
tmp=mul*a[i];
int rem = (k-tmp%k)%k;
if(dp[j].count(rem)) ans+=dp[j][rem];
}
dp[le[i]][a[i]%k]++;
}
cout<<ans<<endl;
}
E. Tree with Small Distances
题意
- 这道题还没想出来,据说使用贪心,下面是别人代码(感觉大佬的思路都是一样,应该是触摸到问题本质)
代码
#include<bits/stdc++.h>
using namespace std;
int n;
const int maxn = 2e5+100;
vector<int> G[maxn];
int used[maxn];
int d[maxn];
int ans = 0;
void dfs(int u,int f){
//cout<<"enter"<<u<<" "<<f<<endl;
bool flag = false;
for(int i=0;i<G[u].size();i++){
int v = G[u][i];
if(v!=f){
d[v] = d[u] + 1;
dfs(v,u);
flag|=used[v];
}
}
if(d[u]>2&&!used[u]&&!flag&&!used[f]){
used[f] = true;
//cout<<"db"<<f<<endl;
ans++;
}
//cout<<"out"<<endl;
}
int main(){
int t;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1,0);
/*
cout<<"db1\n";
for(int i=1;i<=n;i++){
cout<<d[i]<<"\t";
}
cout<<endl;
for(int i=1;i<=n;i++){
cout<<used[i]<<"\t";
}
cout<<"db over"<<endl;
*/
printf("%d\n",ans);
//fclose(stdin);
return 0;
}
F. Multicolored Markers
题意
- 自己看题吧
- 枚举较短边,考虑蓝方块构成矩形的最低高度和红方块构成矩形的最低高度是否和当前高度冲突
代码
#include<bits/stdc++.h>
#define ll long long
int main(){
ll a,b;
std::cin>>a>>b;
ll c=a+b;
ll tmp=std::max(a,b);
ll minl=(a+b+1)*2;
ll lowa=a;
ll lowb=b;
for(ll i=1;i*i<=(a+b);i++){
if(a%i==0) lowa=a/i;
if(b%i==0) lowb=b/i;
if(c%i==0){
if(c/i>=lowa||c/i>=lowb){
//std::cout<<"db"<<i<<std::endl;
minl=std::min(2*(i+c/i),minl);
}
}
}
std::cout<<minl<<std::endl;
}
加油,需要补题Kmp,数论+树
Codeforces Round #506 (Div. 3) D-F的更多相关文章
- Codeforces Round #506 (Div. 3) 1029 F. Multicolored Markers
CF-1029F 题意: a,b个小正方形构造一个矩形,大小为(a+b),并且要求其中要么a个小正方形是矩形,要么b个小正方形是矩形. 思路: 之前在想要分a,b是否为奇数讨论,后来发现根本不需要.只 ...
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- Codeforces Round #506 (Div. 3) E
Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...
- Codeforces Round #573 (Div. 1) 差F
Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...
- Codeforces Round #600 (Div. 2)E F
题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...
- Codeforces Round #346 (Div. 2) E F
因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...
- Codeforces Round #506 (Div. 3)
题解: div3水的没有什么意思 abc就不说了 d题比较显然的就是用hash 但是不能直接搞 所以我们要枚举他后面那个数的位数 然后用map判断就可以了 刚开始没搞清楚数据范围写了快速乘竟然被hac ...
随机推荐
- 安装sublimeServer插件
1.安装目的 做练习在谷歌浏览器中遇到报错信息:axios.min.js:8 Failed to load file:///E:/%E8%87%AA%E5%AD%A6/vue%E5%AD%A6%E4% ...
- js提示是否删除
第一种: <a href="javascript:if(confirm('确认删除吗?'))window.location='del.asp'">删除</a> ...
- python数据分析------文本挖掘(jieba)
1.import jieba jieba的cut函数有三个模式:全模式.精准模式.搜索引擎模式 1 精确模式,试图将句子最精确地切开,适合文本分析: 2 全模式,把句子中所有的可以成词的词语都扫描出来 ...
- buntu Rhythmbox解决中文乱码
Ubuntu Rhythmbox解决中文乱码 在这里介绍的是一个解决方法,修改变量. 在终端输入: gedit ~/.profile 在最后加入下面内容: exportGST_ID3_TAG_ENCO ...
- win7下UDL文件不同
win7 执行UDL文件看不全all驱动.所以没有办法配置数据库的连接.查度娘,方法如下: 在C:\建一个test.udl 文件,运行命令 C:\Windows\syswow64\rundll32.e ...
- appium运行from appium import webdriver 提示most recent call last
这是因为首次启动提示没有APPIUM 模块,CMD 执行 :pip3 install Appium-Python-Client
- ExtJs之Ext.Template
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- LCA【模板】
#include <algorithm> #include <cstdio> #include <vector> #define N 10015 using nam ...
- Mysql 下DELETE操作表别名问题
在用DELETE删除mysql数据库数据时采取一下两种方式: 方式一:DELETE FROM B_PROSON WHERE ID = 1; 不使用别名 方式二:DELETE BP FROM B_PRO ...
- arp与免费arp的差别,arp老化
免费arp:应用场景: case1:PC通过DHCP申请地址.在获取到IP地址后,会发送免费ARP,目的用于探測同一网段时候存在同样的IP地址终端,防止IP冲突. case2:PC的MAC地址发生变化 ...