数学题--On Sum of Fractions
题目链接
题目意思:
水一下
题目意思:
定义v(n)是不超过n的最大素数, u(n)是大于n的最小素数。
以分数形式"p/q"输出 sigma(i = 2 to n) (1 / (v(i)*u(i))), pq为互质整数且q > 0。
通常会想到这个裂项。公式一写发现约掉了许多。
最终是:
所以:AC 注意乘的过程会爆int。
#include <cstdio> #include <cstring> #include <cctype> #include <cmath> #include <set> #include <map> #include <list> #include <queue> #include <deque> #include <stack> #include <string> #include <vector> #include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> using namespace std; typedef long long LL; const int INF=2e9+1e8; const int MOD=1e9+7; const int MAXSIZE=1e6+5; const double eps=0.0000000001; void fre() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); } #define memst(a,b) memset(a,b,sizeof(a)) #define fr(i,a,n) for(int i=a;i<n;i++) bool isprime(int n) { for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } LL gcd(LL a,LL b) { return __gcd(a,b); } int main(int argc,char *argv[]) { int ncase; scanf("%d",&ncase); while(ncase--) { int n; scanf("%d",&n); int z,m; z=m=n; m++; while(!isprime(z)) z--; while(!isprime(m)) m++; LL fz,fm; fz=1ll*z*m-2ll*z-2ll*(m-n-1ll); fm=2ll*z*m; LL temp=gcd(fz,fm); cout<<fz/temp<<"/"<<fm/temp<<endl; } return 0; } /**************************************************/ /** Copyright Notice **/ /** writer: wurong **/ /** school: nyist **/ /** blog : http://blog.csdn.net/wr_technology **/ /**************************************************/
水一下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 如何开发 MCP 服务?保姆级教程!
· 1.net core 工作流WorkFlow流程(介绍)
· 瞧瞧别人家的限流,那叫一个优雅!
· C# 工业视觉开发必刷20道 Halcon 面试题
· 从零散笔记到结构化知识库:我的文档网站建设之路