Strange Sum

题目大意:

对于一个大于$1$的正整数$x$,定义$v(x)$为不超过$x$的最大质数,$u(x)$为大于$x$的最小质数。

给定$n(n\le10^9)$,求:
$$\sum_{i=2}^n\frac{1}{v(i)\cdot u(i)}$$

多组数据,要求按最简分数形式输出。

分析:

对于任意两个相邻质数$p_1$、$p_2$,取任意整数$x\in[p_1,p_2)$,都满足:$\frac{1}{v(i)\cdot u(i)} = \frac{1}{p_1\cdot p_2}$,即这些数的和为$\frac{p_2-p_1}{p_1\cdot p_2} = \frac{1}{p_1} - \frac{1}{p_2}$。

那么,当$n+1$为质数时,设$P_i$为不大于$n+1$的质数,则
$$\sum_{i=2}^n\frac{1}{v(i)\cdot u(i)}=\sum_{i=1}^{k}(\frac{1}{p_i}-\frac{1}{p_i+1})=\frac{1}{2}-\frac{1}{n+1}$$

而当$n$不为质数时,找到小于$n$的最大质数$P$,计算出$[2,P-1]$区间内的答案,$[P,n]$区间内的答案暴力求就好了。

代码:

 1 #include <cstdio>
 2
 3 int t, n, f[100010], p[100000], pn;
 4 int p1, p2;
 5 long long fm, fz;
 6
 7 inline bool isPrime(int x)
 8 {
 9     if (x <= 100000)
10     {
11         return f[x] == 0;
12     }
13     for (int i = 0, j; i < pn; i++)
14     {
15         j = p[i];
16         if (j * j > x)
17         {
18             break;
19         }
20         if (x % j == 0)
21         {
22             return 0;
23         }
24     }
25     return 1;
26 }
27
28 long long gcd(long long a, long long b)
29 {
30     return b == 0 ? a : gcd(b, a % b);
31 }
32
33 int main()
34 {
35     f[0] = f[1] = 1;
36     pn = 0;
37     for (int i = 2; i <= 100000; i++)
38     {
39         if (f[i] == 0)
40         {
41             p[pn++] = i;
42             for (int j = i + i; j <= 100000; j += i)
43             {
44                 f[j] = i;
45             }
46         }
47     }
48     scanf("%d", &t);
49     while (t--)
50     {
51         scanf("%d", &n);
52         for (p1 = n; p1 > 1; p1--)
53         {
54             if (isPrime (p1) == 1)
55             {
56                 break;
57             }
58         }
59         for (p2 = n + 1; p2 < 1000000010; p2++)
60         {
61             if (isPrime (p2) == 1)
62             {
63                 break;
64             }
65         }
66         fm = (long long) p1 * p2 * 2;
67         fz = (long long) p2 * (p1 - 2) + (long long) (n - p1 + 1) * 2;
68         long long k = gcd(fz, fm);
69         fz /= k;
70         fm /= k;
71         printf("%lld/%lld\n", fz, fm);
72     }
73 }
时间: 2024-10-15 17:42:03

Strange Sum的相关文章

HDU - 5198 - Strange Class &amp;&amp; 5199 - Gunner

Strange Class Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 74    Accepted Submission(s): 60 Problem Description In Vivid's school, there is a strange class(SC). In SC, the students' names ar

hdu 5152 A Strange Problem线段树+欧拉函数

*****************************************BC题解**********************************************************************1003 A Strange Problem 对于操作二,利用公式 当x >= Phi(C), A^x = A ^ (x%Phi(C) + Phi(C)) (mod C) 对于2333333这个模数来说,求18次欧拉函数后就变成了1,所以只需要保存19层第三次操作的加数

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种

31.SUM() 函数

SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM(column_name) FROM table_name SQL SUM() 实例 我们拥有下面这个 "Orders" 表: O_Id OrderDate OrderPrice Customer 1 2008/12/29 1000 Bush 2 2008/11/23 1600 Carter 3 2008/10/05 700 Bush 4 2008/09/28 300 Bush 5

1305 Pairwise Sum and Divide

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) return sum 给出数组A,由你来计算fun(A)的结果.例如:A = {1, 4, 1},fun(A) = [5/4] + [

Java [Leetcode 303]Range Sum Query - Immutable

题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You may assume that the ar

LeetCode 303. Range Sum Query - Immutable

求数组nums[i,j]的和 思路:另开一sum数组,sum[i]为nums[0,i]的和,所以nums[i,j] = sum[j] - sum[i-1] 1 class NumArray { 2 public: 3 vector<int> sum; 4 NumArray(vector<int> &nums) { 5 sum.resize(nums.size(), 0); 6 sum[0] = nums[0]; 7 int len = nums.size(); 8 for(