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. 科学计算库Numpy——文件读写

    读文件 要读取的文件 有分隔符的文件 备注:delimiter分隔符. 有多余行的文件 备注:skiprows去掉几行. 指定列 备注:usecols指定使用哪几列. 写文件 保存后的文件 备注:fm ...

  2. [Hdu1693]Eat the Trees(插头DP)

    Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...

  3. sql中比较大小

    if object_id('tempdb..#dataOldNew1') is not null drop table #dataOldNew1 select distinct store_cd ,i ...

  4. vba通过函数调用Winrar压缩软件压缩文件

    Dim site As String        Dim FolderName As String        Dim FolderName1 As String        Dim Rarex ...

  5. loj6392 「THUPC2018」密码学第三次小作业 / Rsa

    还是挺好做的,\((e_1,e_2)=1 \Rightarrow e_1s+e_2t=0\),\(m \equiv m^1 \equiv m^{e_1s+e_2t} \equiv c_1^s c_2^ ...

  6. 一个漂亮的PHP验证码

    自己导入字体,可以按照自己的额需要随便修改. <?php class Imagecode{ private $width ; private $height; private $counts; ...

  7. 递归查询子类sql

    --通过父节点查询子节点 WITH TREE AS( SELECT * FROM Role WHERE RoleID = 4 -- 要查询的父 id UNION ALL SELECT Role.* F ...

  8. 【Longest Substring Without Repeating Characters】cpp

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. C++ STL map容器的说明测试1

    // maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...

  10. Python+Selenium练习篇之13-获取当前页面的URL

    本文介绍如何通过webdriver方法获取当前测试页面的URL.获取当前URL有什么用处呢,一般URL可以帮助我们判断跳转的页面是否正确,或者URL中部分字段可以作为我们自动化测试脚本期待结果的一部分 ...