↓ Skip to main content
  1. Posts/

【叉姐的魔法训练第一课_初级魔法练习】poj 3244 Difference between Triplets (数学)

·565 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:
#

For every pair of triplets, Ta = (Ia, Ja, Ka) and T__b = (Ib, Jb, Kb), we define the difference value between Ta and_T__b_ as follows:

D(Ta,_ Tb_) = max {Ia − Ib, Ja − Jb, Ka − Kb} − min {Ia − Ib, Ja − Jb, Ka − Kb}

Now you are given N triplets, could you write a program to calculate the sum of the difference values between every unordered pair of triplets?

思路:转化要求的式子,如果把_Ia_ − Ib, Ja − Jb, Ka − Kb 看成数轴上的点,所求的式子就变成了求三个点构成的线段的距离。
#

**设X=Ia − Ib,Y=Ja − Jb,Z=Ka − Kb,那么该距离D = (|X-Y| + |Y-Z| + |Z-X| )/2(该式子是此题最关键的一部,可以通过画图直观得到) **
#

D=|(ia-ja)-(ib-jb)| + |(ja-ka)-(jb-kb)| + | (ka-za)+(kb-zb) |
#

设a = ia-ja,b =ja-ka,c = ka-ia,然后分别排序类似于bzoj1604_拆点求曼哈顿距离
#

考虑第i个a,对于其他的n-1个a,有i-1个比它小,n-i个比它大,因此对答案的贡献为(i-1)个a[i]和 (n-i)个-a[i]
#

b,c同理。
#

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年11月17日 星期四 20时05分54秒
 4File Name :code/hdu/3244.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31const int N=2E5+7;
32int n;
33int x,y,z;
34int a[N],b[N],c[N];
35int main()
36{
37	#ifndef  ONLINE_JUDGE
38	freopen("code/in.txt","r",stdin);
39  #endif
40	while (~scanf("%d",&n))
41	{
42	    if (n==0) break;
43	    for ( int i = 1; i  <= n ; i++)
44	    {
45		scanf("%d%d%d",&x,&y,&z);
46		a[i] = x - y;
47		b[i] = y - z;
48		c[i] = z - x;
49	    }
50	    sort(a+1,a+n+1);
51	    sort(b+1,b+n+1);
52	    sort(c+1,c+n+1);
53	    LL ans = 0 ;
54	    for (  int i = 1 ; i <= n ; i++)
55	    {
56		ans += 1LL*(i-1)*a[i];
57		ans +=-1LL*(n-i)*a[i];
58		ans += 1LL*(i-1)*b[i];
59		ans +=-1LL*(n-i)*b[i];
60		ans += 1LL*(i-1)*c[i];
61		ans +=-1LL*(n-i)*c[i];
62	    }
63	    printf("%lld\n",ans/2);
64	}
65  #ifndef ONLINE_JUDGE
66  fclose(stdin);
67  #endif
68    return 0;
69}

#

#

Related

light oj 1045 Digits of Factorial (k进制数的位数)

·276 words·1 min
题目链接 题意:求n!在k进制表示下有多少位。 思路:答案为[ log(1)+log(2)+…+log(N) ]+1 其中log的底数都是K 由于有多组数据,预处理一个log的前缀和。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :Tue 13 Sep 2016 05:13:15 PM CST 4File Name :code/loj/1045.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <set>-digits-of-factorial-k进制数的位 13#include <map> 14#include <string> 15#include <cmath> 16#include <cstdlib> 17#include <ctime> 18#define fst first 19#define sec second 20#define lson l,m,rt<<1 21#define rson m+1,r,rt<<1|1 22#define ms(a,x) memset(a,x,sizeof(a)) 23typedef long long LL; 24#define pi pair < int ,int > 25#define MP make_pair 26using namespace std; 27const double eps = 1E-8; 28const int dx4[4]={1,0,0,-1}; 29const int dy4[4]={0,-1,1,0}; 30const int inf = 0x3f3f3f3f; 31const int N=1E6+7; 32int n; 33int base; 34double sum[N]; 35int main() 36{ 37 #ifndef ONLINE_JUDGE 38 freopen("code/in.txt","r",stdin); 39 #endif 40 sum[0] = 0 ; 41 for ( int i = 1 ; i < N ; i++) sum[i] = sum[i-1] + log(i); 42 int T; 43 cin>>T; 44 int cas = 0 ; 45 while (T--) 46 { 47 scanf("%d%d",&n,&base); 48 double ans = sum[n]/log(base)+1; 49 printf("Case %d: %d\n",++cas,int(ans)); 50 } 51 #ifndef ONLINE_JUDGE 52 fclose(stdin); 53 #endif 54 return 0; 55}

codeforces #368 div 2 C. Pythagorean Triples (构造,数学)

·953 words·2 mins
题目链接 题意:给出一个数,问包含这个数三个数组成的勾股数,输出另外两个数。 思路: 所谓勾股数,就是当组成一个直角三角形的三边长都为正整数时,我们就称这一组数为勾股数. 那么,组成一组勾股数的三个正整数之间,是否具有一定的规律可寻呢?下面我们一起来观察几组勾股数: 规律一:在勾股数(3,4,5)、(5,12,13)、(7,24,25)(9,40,41)中,我们发现 由(3,4,5)有:32=9=4+5 由(5,12,13)有:52=25=12+13 由(7,24,25)有:72=49=24+25 由(9,40,41)有:92=81=40+41. 即在一组勾股数中,当最小边为奇数时,它的平方刚好等于另外两个连续的正整数之和.因此,我们把它推广到一般,从而可得出以下公式: ∵(2n+1)²=4n²+4n+1=(2n²+2n)+(2n²+2n+1) ∴(2n+1)²+(2n²+2n)²=(2n²+2n+1)²(n为正整数) 证明(略) 勾股数公式一:(2n+1,2n²+2n,2n²+2n+1)(n为正整数) 规律二:在勾股数(6,8,10)、(8,15,17)、(10,24,26)中,我们发现 由(6,8,10)有:62=36+2×(8+10) 由(8,15,17)有:82=64=2×(15+17) 由(10,24,26)有:102=100=2×(24+26) 即在一组勾股数中,当最小边为偶数时,它的平方刚好等于两个连续整数之和的二倍,推广到一般,从而可得出另一公式: ∵(2n)2=4n2=2[(n2-1)+(n2+1)] ∴(2n)2+(n2-1)2=(n2+1)2(n≥2且n为正整数) 证明(略) 勾股数公式二:(2n,n²-1,n²+1)(n≥2且n为正整数) 利用以上两个公式,我们可以快速写出各组勾股数.

hdu 5833 || ccpc 2016 网络赛 1002 Zhu and 772002 (高斯消元)

·528 words·2 mins
hdu 5833 题目链接 题意:n个数,保证每个数的素因子不超过2000,从中取若干个,问乘积是完全平方数的方案数。 思路: 完全平方数就是要求每个质因子的指数是偶数次。 列方程组,a1,a2,a3……am分别表示bi是否在集合中。对于每一个素因子,建立异或方程组,要求因子个数为偶数,即异或为0

hdu 2050 折线分割平面 (找规律,递推)

·246 words·1 min
hdu 2050题目链接 题意:n条折线。。最多能把平面分成几部分。。 思路:联想到m条直线,最多能把平面分成m*(m+1)/2+1部分。。 画图发现。。。 f[2*n-1]==g[n]。。

whust2016 warm up A ||codeforces 682 A. Alyona and Numbers (计数问题,水)

·294 words·1 min
cf682A题目链接 题意:两个数组,分别为1..n和1..m。。。从两个数组中各取一个,问和能被5整除的方案数。。。 思路:傻逼题。。。统计%5。。。然后乘法原理。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年07月18日 星期一 12时32分22秒 4File Name :code/2016whust/A.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E6+7; 34int n,m; 35LL a[N],b[N]; 36int main() 37{ 38 #ifndef ONLINE_JUDGE 39 freopen("code/in.txt","r",stdin); 40 #endif 41 42 cin>>n>>m; 43 ms(a,0); 44 ms(b,0); 45 for ( int i = 1 ; i <= n ; i++) 46 { 47 int x = i % 5; 48 a[x]++; 49 } 50 for ( int i = 1 ; i <= m ; i++) 51 { 52 int x = i % 5; 53 b[x]++; 54 } 55 LL ans = 0; 56 ans = a[0]*b[0]+a[1]*b[4]+a[2]*b[3]+a[3]*b[2]+a[4]*b[1]; 57 cout<<ans<<endl; 58 59 #ifndef ONLINE_JUDGE 60 fclose(stdin); 61 #endif 62 return 0; 63}