f(n)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 457    Accepted Submission(s): 279

Problem Description
This time I need you to calculate the f(n) . (3<=n<=1000000)

f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).
Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])
C[n][k] means the number of way to choose k things from n some things.
gcd(a,b) means the greatest common divisor of a and b.

 
Input
There are several test case. For each test case:One integer n(3<=n<=1000000). The end of the in put file is EOF.
 
Output
For each test case:
The output consists of one line with one integer f(n).
 
Sample Input
3
26983
 
Sample Output
3 37556486
这题要用到这个公式:
,那么G的值为:


n为素数:本身

n有多个素因子:1

n只有一个素因子:该因子

920MS才跑过去。。没优化了。。记得用_int64保存结果。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL; const int N = ;
bool p[N];
LL g[N];
LL f[N];
void init(){
memset(g,,sizeof(g));
for(int i=;i<N;i++){
if(!p[i]){
g[i]=(LL)i;
for(LL j = (LL)i*i;j<N;j+=i){
p[j] = true;
}
}
}
f[]=;
for(int i=;i<N;i++){
int cnt = ;
if(g[i]==){
int temp=-;
int n = i;
for(int j=;j*j<=n;j++){
if(n%j==){
temp=(LL)j;
cnt++;
while(n%j==){
n/=j;
}
}
}
if(n>) {temp=(LL)n,cnt++;}
if(cnt!=) g[i]=;
if(cnt==) g[i]=(LL)temp;
}
f[i]=f[i-]+g[i];
}
}
int main()
{
init();
int n;
while(~scanf("%d",&n)){
printf("%lld\n",f[n]);
}
return ;
}

hdu 2582(数论相关定理+素数筛选+整数分解)的更多相关文章

  1. hdu 2685(数论相关定理+欧几里德定理+快速取模)

    I won't tell you this is about number theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  2. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  3. hdu 2136(质数筛选+整数分解)

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  5. HDU 2161 Primes (素数筛选法)

    题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostrea ...

  6. CPC23-4-K. 喵喵的神数 (数论 Lucas定理)

    喵喵的神∙数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活 ...

  7. HDU4548美素数——筛选法与空间换时间

    对于数论的学习比较的碎片化,所以开了一篇随笔来记录一下学习中遇到的一些坑,主要通过题目来讲解 本题围绕:素数筛选法与空间换时间 HDU4548美素数 题目描述 小明对数的研究比较热爱,一谈到数,脑子里 ...

  8. acm数论之旅--数论四大定理

    ACM数论之旅5---数论四大定理(你怕不怕(☆゚∀゚)老实告诉我)   (本篇无证明,想要证明的去找度娘)o(*≧▽≦)ツ ----------数论四大定理--------- 数论四大定理: 1.威 ...

  9. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

随机推荐

  1. GTF/GFF

  2. VMWare workstation Pro 14 For Linux key

    VMWare workstation Pro 14 For Linux key: (我使用的Linux 系统是 Ubuntu16.04, 64位 ) 镜像是官方网址下载的,你也可以自己去官方网址下载: ...

  3. CodeForces:#448 div2 B. XK Segments

    传送门:http://codeforces.com/contest/895/problem/B B. XK Segments time limit per test1 second memory li ...

  4. P1616 疯狂的采药【模板】

    此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师. ...

  5. utf8和utf8mb4区别

    原文链接 一.简介 MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超集,除了将编 ...

  6. datagrid的toolbar的两种实现方式

    datagrid的toolbar的两种实现方式 1.在html文件中,可以在设置toolbar="#tb",再在div中设置id="tb" <table ...

  7. 史上最权威的 Activiti 框架学习

        Activiti5 是 由 Alfresco 软件在 2010 年 5 月 17 日发布的业务流程管理( BPM) 框架,它是覆盖了业务流程管理.工作流.服务协作等领域 的一个开源的.灵活的. ...

  8. Python框架之Django学习笔记(九)

    模型 之前,我们用 Django 建造网站的基本途径: 建立视图和 URLConf . 正如我们所阐述的,视图负责处理一些主观逻辑,然后返回响应结果. 作为例子之一,我们的主观逻辑是要计算当前的日期和 ...

  9. Oracle 学习笔记(十)

    合并查询 在实际项目开发中经常遇到要合并结果集的情况,可以使用集合操作符:union,union all,intersect,minus.这次笔记学习这几个操作符. [union] 该操作符用于取得两 ...

  10. SQL语句Not IN优化方案

    总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查.    select * from emp where emp_no not in (select emp_no from emp ...