SPOJ BALLSUM - Ball sum

题目链接http://www.spoj.com/problems/BALLSUM/

题目大意:问从N个数中选两个数和小于等于K的概率值,用分数表示。

解题思路:假设要选择小于等于5的数字,那么可选项有(1,4) (1,3) (1,2) (2,3),可以发现实际上是k-2 + k-4 +...当该项为0时停止即可。N个数里面选择两个可以直接n(n-1)/2,表示成分数则可以同除以GCD。

代码:

 1 const int maxn = 1e6 + 5;
 2 ll n, k;
 3
 4 ll gcd(ll a, ll b){
 5     return b == 0? a: gcd(b, a % b);
 6 }
 7 void solve(){
 8     ll x = k - 2, up = 0;
 9     while(x > 0){
10         up += x;
11         x -= 2;
12     }
13     if(up == 0){
14         puts("0");
15         return;
16     }
17     ll down = n * (n - 1) / 2;
18     ll g = gcd(up, down);
19     up /= g; down /= g;
20     printf("%lld/%lld\n", up, down);
21     return;
22 }
23 int main(){
24     while(scanf("%lld %lld", &n, &k) && n != -1){
25         solve();
26     }
27 }

题目:

BALLSUM - Ball sum

#simple-math #math #probability-theory #gcd

You have a bag filled with N balls.Each Ball has a distinct number from 1 to N printed on it.All the numbers are distinct. You withdraw two balls from the bag and take their sum. You need to calculate the probability that the sum is not greater than the given number K(<=N). The Answer should be displayed in the form of p/q.(except when the answer is 0 or 1)

Input

Input consists of various test cases. Each test case consist of two integer inputs,N and K. (0<=K<=N<=1000000000) The program stops taking input when N and K equals -1

Output

Output the result in the form of p/q.(Except when the answer is 0 or 1)

Example

Input:
3 2
100 5
10 6
-1 -1

Output:
0
2/2475
2/15
时间: 2024-10-27 02:21:05

SPOJ BALLSUM - Ball sum的相关文章

[数位统计] spoj 1433 The sum

题意:对于数加一位减一位,给定N,求1~N的和. 例子12=1-2+3-4......-1+2=5 思路: 个人思路可能比较复杂,但是思路还是比较清晰的. 首先我们把一个数N分成两个部分,比如4568=1~999+1000~4568,567=1~99+100~567 也就是整百整千的数我们可以递推算出来,虽然找规律也是可以的~ 就是1~999=1~99+100~999. 这样算的话 其实就解决了最后计算的问题,更能统一函数. 然后其实我们可以发现,位数是奇数的数,除了个位前面的数是两两抵消的.

SPOJ LCMSUM - LCM Sum

题意是求: $\sum_{i = 1}^{n}lcm(i, n)$ $= \sum_{i = 1}^{n}\frac{ni}{gcd(i, n)}$ $= n\sum_{i = 1}^{n}\frac{i}{gcd(i, n)}$ $= n\sum_{d|n}\sum_{i = 1}^{n}d*[gcd(i, n)==d]$ $= n\sum_{d|n}\sum_{i = 1}^{\frac{n}{d}}i*[gcd(i, \frac{n}{d})==1]$ $= n\sum_{d|n}\sum

java String的比较,BOX装箱拆箱,以及面向对象的小代码

package cn.hncu.day2; public class StringDemo { public static void main(String[] args) { String str1 = "abc";//直接赋值,放在栈中,数据共享 String str2 = "abc"; System.out.println("str1==str2: "+(str1==str2));//true String str3 = new Strin

SPOJ 11840. Sum of Squares with Segment Tree (线段树,区间更新)

http://www.spoj.com/problems/SEGSQRSS/ SPOJ Problem Set (classical) 11840. Sum of Squares with Segment Tree Problem code: SEGSQRSS Segment trees are extremely useful.  In particular "Lazy Propagation" (i.e. see here, for example) allows one to c

SPOJ CPCRC1C Sum of Digits

题目连接 题意:计算从a到b每个数每位数字相加的和 code: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; typedef unsigned long long ll; ll cnt[16]; //cnt[i]表示0 到 最大n位数的个位数的和 ll s[16]; //s[i]表示10的i次方 ll num[1

SPOJ CRAN02 - Roommate Agreement

题目链接:http://www.spoj.com/problems/CRAN02/ 题目大意:N个数字组成的序列,和为0的连续子序列的个数.N<1e6 解题思路:计算前缀和,统计每个数字出现的次数,那么对于数字sum[i], 如果存在k个sum[i],则代表有C(k, 2)个序列和为0,而如果sum[i] = 0,则还要累加上对应的k值. 代码: 1 ll n; 2 int a[maxn]; 3 ll sum[maxn]; 4 map<int, int> mmp; 5 6 void so

BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca

2588: Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文. Input 第一行两个整数N,M. 第二行有N个整数,其中第i个整数表示点i的权值. 后面N-1行每行两个整数(x,y),表示点x到点y有一条边. 最后M行每行两个整数(u,v,k),表示一组询问.

Color the ball(树状数组+线段树)

Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b

BZOJ 2226: [Spoj 5971] LCMSum( 数论 )

∑lcm(i,n) = ∑ i*n/(i,n) = ∑d|n∑(x,n)=d x*n/d = ∑d|n∑(t,n/d)=1t*n = n∑d|nf(d). f(d)表示1~d中与d互质的数的和, 即f(d) = d*φ(d)/2(d>=2). 然后O(n)筛φ, 每次询问暴力算即可...最大是100w,sqrt(100w)=1000内的质数是168个, 所以复杂度是O(n + T*168), 可以AC  ----------------------------------------------