HDU 5382 GCD?LCM! (递推公式 + 打表)

GCD?LCM!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 192    Accepted Submission(s): 105


Problem Description
First we define:
(1)  lcm(a,b) , the least common multiple of two integers  a  and  b , is the smallest positive integer that is divisible by both  a  and  b . for example,  lcm(2,3)=6  and  lcm(4,6)=12 .
(2)  gcd(a,b) , the greatest common divisor of two integers  a  and  b , is the largest positive integer that divides both  a  and  b  without a remainder,  gcd(2,3)=1  and  gcd(4,6)=2 .
(3)  [exp] exp  is a logical expression, if the result of  exp  is  true , then  [exp]=1 , else  [exp]=0 . for example,  [1+23]=1  and  [1+24]=0 .

Now Stilwell wants to calculate such a problem:
F(n)=i=1nj=1n [ lcm(i,j)+gcd(i,j)n ]S(n)=i=1nF(i)

Find  S(n) mod 258280327 .
 

Input
The first line of the input contains a single number  T , the number of test cases.
Next  T  lines, each line contains a positive integer  n .
T105 n106 .
 

Output
T  lines, find  S(n) mod 258280327 .
 

Sample Input
  
  
8 1 2 3 4 10 100 233 11037
 

Sample Output
  
  
1 5 13 26 289 296582 3928449 213582482
 

Author
SXYZ
 

Source
 


题目大意:函数lcm(a,b):求两整数a,b的最小公倍数;函数gcd(a,b):求两整数a,b的最大公约数。函数[exp],其中exp是一个逻辑表达式。如果逻辑表达式exp是真,那么函数[exp]的值是1,否则函数[exp]的值是0。例如:[1+2>=3] = 1 ,[1+2>=4] = 0。

求S(n)的值。




解题思路:

F(n)的递推公式:




T(n)的递推公式:



G(n)的递推公式:


根据上面的递推公式,直接打表计算S(n)。



代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 258280327
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define MAX 1000100
//素数标记为0,非素数标记为1
bool prime[MAX+5];
ll p[MAX+5];
ll cnt,num[MAX+5];
ll G[MAX],T[MAX],F[MAX],S[MAX];
ll quickpow(ll a,ll b)
{
    ll c=1;
    while(b)
    {
        if(b&1)
            c=c*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return c;
}
void getprime()
{
    ll i,j;
    memset(prime,0,sizeof(prime));
    memset(num,0,sizeof(num));
    cnt=0;
    for(i=2;i<=MAX;i++)
    {
        if(prime[i]==0)
        {
            p[cnt++]=i;
            for(j=i;j<=MAX;j+=i)
            {
                num[j]++;
                prime[j]=1;
            }
        }
    }
    G[0]=0;
    for(i=1;i<=MAX;i++)
        G[i]=quickpow(2,num[i]);
    memset(T,0,sizeof(T));
    for(i=1;i<MAX;i++)
    {
        for(j=i;j<MAX;j+=i)
        {
            T[j]=(T[j]+G[j/i-1])%mod;
        }
    }
    memset(F,0,sizeof(F));
    memset(S,0,sizeof(S));
    F[1]=S[1]=1;
    for(int i=2;i<MAX;i++)
    {
        F[i]=(((F[i-1]+2*i-1)%mod-T[i-1])%mod+mod)%mod;
        S[i]=(S[i-1]+F[i])%mod;
    }
}
int main()
{
    getprime();
    ll t,n;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        printf("%I64d\n",S[n]);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值