ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph
ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091
题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1
解法:
因子有2的数: 2,4,6,8,10,12,14.。。
因子有3的数:3,6,9,12,15,18,21.。。
因子有5的数:5,10,15,18,21,24.。。
可以看出这里求出的集合时会有重复的,得去从。可惜没有学过容斥原理。不过解决这题还是没问题的。
50以内的素因子有:2, 3, 5, 7, 11, 13, 17, 19, 23只有这些素因子才可能产生集合元素大于2的集合
排除重复度为2的集合: 6{2,3(因子2和因子3造成集合重复)}, 10{2,5},14{2,7}, 22{2, 11}, 15{3,5},21{3,7}
代码为:
IN = lambda : map(int, raw_input().split() )
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23]
x = [6, 10, 14, 22, 15, 21] k, s = IN()
c =[ [0]*(s+1) for i in xrange(s+1) ]
for i in xrange(s+1):
c[i][1] = i; c[i][0] = 1; c[i][i]=1
for i in xrange(1,s+1):
for j in xrange(1, i):
c[i][j] = c[i-1][j]+c[i-1][j-1] sum = 0
for v in prime:
if s/v<k: break
sum += c[s/v][k]
for v in x:
if s/v<k: break
sum -= c[s/v][k] print sum if sum<10000 else 10000
cf 295B http://codeforces.com/problemset/problem/295/B
题意是:按照一定顺序删除点并删除与点相连的线,求删除该点前的点集合里两两点的最短距离。
这题我以前看到过类似的,很自然就想到了从后往前处理,每次把这个点加进去循环更新距离,这个类似floyed
python代码:肯能是python效率问题吧,这个代码过不了。TLE,但是换成c++就过了
from sys import stdin,stdout
IN = lambda: [ int(x) for x in stdin.readline().split() ]
n = int( stdin.readline().strip() )
edge = []
for i in xrange(n):
edge.append( IN() )
x = IN()
ans = [0]*n for k in xrange(n-1, -1, -1):
for i in xrange(n):
for j in xrange(n):
edge[i][j] = min( edge[i][j], edge[i][x[k] -1] + edge[x[k]-1 ][j] )
for i in xrange(k, n):
for j in xrange(k, n):
ans[k] += edge[x[i]-1 ][x[j]-1 ]
print ' '.join( map(str,ans ) )
c++ code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define maxn 505
int n, edge[maxn][maxn];
int x[maxn];
long long ans[maxn]; int main(int argc, char**argv){
cin >> n;
for ( int i=; i<n; ++i )
for ( int j=; j<n; ++j )
cin >> edge[i][j];
for ( int i=; i<n; ++i ) cin >>x[i];
for ( int k=n-; k>=; --k ){
for ( int i=; i<n; ++i )
for ( int j=; j<n; ++j )
edge[i][j] = min( edge[i][j], edge[i][x[k]- ]+ edge[x[k]-][j] );
ans[k] = ;
for ( int i=k; i<n; ++i )
for ( int j=i+; j<n; ++j )
ans[k] += edge[x[i]- ][x[j]- ]+edge[x[j]- ][x[i]- ];
}
for ( int i=; i<n; ++i )
printf("%I64d ", ans[i]);
}
ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph的更多相关文章
- ural 1091. Tmutarakan Exams(容斥原理)
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- Ural 1091 Tmutarakan Exams
Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
- Codeforces 295 B. Greg and Graph
http://codeforces.com/problemset/problem/295/B 题意: 给定一个有边权的有向图.再给定一个1~n的排列. 按排列中的顺序依次删除点,问每次删除后,所有点对 ...
- ural 1091. Tmutarakan Exams(容斥)
http://acm.timus.ru/problem.aspx? space=1&num=1091 从1~s中选出k个数,使得k个数的最大公约数大于1,问这种取法有多少种. (2<=k ...
- URAL - 1091 Tmutarakan Exams (简单容斥原理)
题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1.求这样的数的个数 分析:从2开始枚举gcd,但这样会发生重复.譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就 ...
- 1091. Tmutarakan Exams
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- 容斥原理--计算并集的元素个数 URAL 1091
在计数时,必须注意没有重复,没有遗漏.为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计 ...
- F - Tmutarakan Exams URAL - 1091 -莫比乌斯函数-容斥 or DP计数
F - Tmutarakan Exams 题意 : 从 < = S 的 数 中 选 出 K 个 不 同 的 数 并 且 gcd > 1 .求方案数. 思路 :记 录 一 下 每 个 数 的 ...
- 2014 Super Training #3 H Tmutarakan Exams --容斥原理
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
随机推荐
- 肾果手机App Store切换区域(无需Visa或者万事达)
8月份在肾果官网买了个touch6,有时候需要换区去墙外下载app,然而一个个国家都要输入Visa或者万事达卡...今天终于找到一个不用输入信用卡号的区域:Canada!!! 办法(适用于8.X,7. ...
- javascript移动设备触屏事件
ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件: /** * onTouchEvent */ var div = d ...
- SecureCRT+WinSCP 共用 key pub 密钥 转换 ppk 登录ssh
使用SecureCRT生成的密钥,无法在WinSCP使用, 使用puttygen.exe无法直接转换,解决办法 1.使用大于等于SecureCRT6.5版本,来转换 记得放入私钥,不是pub公钥.然后 ...
- uva 1475 - Jungle Outpost
半平面交,二分: 注意,题目的点是顺时针给出的: #include<cstdio> #include<algorithm> #include<cmath> #def ...
- php nl2br() 函数
nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />).
- php邮件发送 phpmailer
首先要安装phpmailer开源项目. 将class.phpmailer.php转移到php文件夹下, 编写代码: <?php require("class.phpmailer.php ...
- Linux下使用clock_gettime给程序计时
http://www.cnblogs.com/daqiwancheng/archive/2010/07/01/1769522.html
- Altium Designer完美双屏显示方法演示
布线时我们往往需要对一些信号线做特别的走线处理,这样需要边布线边对照原理图,在protel99中那是一个很痛苦的事,在Altium Designer中这种情况将变很简单. 硬件要求,笔记本+外接显示器 ...
- PieTTY
PieTTY 用 pietty 連上主機時 鍵盤右方數字鍵 (keypad) 失效的問題 用 pietty 連上主機時 鍵盤右方數字鍵 (keypad) 失效的問題 應該滿多人用 pietty 連上程 ...
- 对加密方式(公钥私钥)的形象理解(以http和https为例)
https其实就是建构在SSL/TLS之上的 http协议,所以要比较https比http多用多少服务器资源,主要看SSL/TLS本身消耗多少服务器资源. http使用TCP 三次握手建立连接,客户端 ...