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. 学习Pytbon第三天,用户输入

    _username ='dream' #定义用户名 _password ='dream123'#定义用户密码username = input("username:")#请输入用户名 ...

  2. while循环中continue和break的区别

    除了满足while条件外,还有两种方法可以终止循环,它们分别是break和continue.它们唯一的区别是break跳出整个循环,直接执行下面的代码了;而continue是终止当次循环,不执行下面的 ...

  3. [Hdu3555] Bomb(数位DP)

    Description 题意就是找0到N有多少个数中含有49. \(1\leq N \leq2^{63}-1\) Solution 数位DP,与hdu3652类似 \(F[i][state]\)表示位 ...

  4. TI C64X+通用库函数使用手册

    在使用前,当知悉以下几点: 函数进程由手动汇编而成,已充分发挥器件效率.同时TI对外提供C和线性汇编代码 对于个人一些特殊应用,DSPLIB可能会带来额外的cycle消耗 TI DSPLIB依平台和时 ...

  5. POJ 2763 Housewife Wind 树链拋分

    一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...

  6. TCP/IP网络编程之多线程服务端的实现(二)

    线程存在的问题和临界区 上一章TCP/IP网络编程之多线程服务端的实现(一)的thread4.c中,我们发现多线程对同一变量进行加减,最后的结果居然不是我们预料之内的.其实,如果多执行几次程序,会发现 ...

  7. Helloworld 在jvm 内存图

    HelloWorld.java源码如下:   public class HelloWorld { public static void main(String[] args) { String s ; ...

  8. springcloud 高可用分布式配置中心

    SpringCloud教程七:高可用的分布式配置中心(SpringCloud Config) 当服务有很多 都要从服务中心获取配置时 这是可以将服务中心分布式处理 是系统具备在集群下的大数据处理 主要 ...

  9. 34条简单的SQL优化准则

    转载地址:http://bbs.csdn.net/topics/260002113 我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享!(1)    ...

  10. redhat linux 7.4关闭透明大页

    每一步: 在GRUB_CMDLINE_LINUX加入选项 transparent_hugepage=never echo 'GRUB_CMDLINE_LINUX="transparent_h ...