hdu 5382 GCD?LCM!
先考虑化简f函数
发现,f函数可以写成一个递归式,化简后可以先递推求出所有f函数的值,
所以可以先求出所有S函数的值,对于询问,O(1)回答
代码:
//File Name: hdu5382.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年10月24日 星期一 11时03分18秒 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define LL long long
using namespace std;
const int MAXN = + ;
const int MOD = ;
LL S[MAXN],f[MAXN],g[MAXN],h[MAXN];
int prime[MAXN];
bool check[MAXN];
void cal_h(){
memset(check,false,sizeof(check));
h[] = ;
int tot = ;
for(int i=;i<MAXN;i++){
if(!check[i]){
prime[tot++] = i;
h[i] = ;
}
for(int j=;j<tot;j++){
if((LL)i * prime[j] >= MAXN) break;
check[i * prime[j]] = true;
if(i % prime[j] == ){
h[i * prime[j]] = h[i];
break;
}
else
h[i * prime[j]] = h[i] * % MOD;
}
}
}
void cal_g(){
for(int i=;i<MAXN;i++){
for(int j=i;j<MAXN;j+=i){
(g[j] += h[j / i - ]) %= MOD;
}
}
}
void cal_f(){
for(int i=;i<MAXN;i++)
(f[i] = f[i-] + * i - - g[i - ] + MOD) %= MOD;
}
void cal_S(){
for(int i=;i<MAXN;i++)
S[i] = (S[i-] + f[i]) % MOD;
}
void init(){
cal_h();
cal_g();
cal_f();
cal_S();
}
int main(){
init();
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%lld\n",S[n]);
}
return ;
}
hdu 5382 GCD?LCM!的更多相关文章
- 2015多校第8场 HDU 5382 GCD?LCM! 数论公式推导
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5382 题意:函数lcm(a,b):求两整数a,b的最小公倍数:函数gcd(a,b):求两整数a,b的最 ...
- hdu 5382 GCD?LCM! - 莫比乌斯反演
题目传送门 传送门I 传送门II 题目大意 设$F(n) = \sum_{i = 1}^{n}\sum_{j = 1}^{n}\left [ [i, j] + (i, j) \geqslant n \ ...
- 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)
Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...
- hdu 5584 gcd/lcm/数学公式
input T 1<=T<=1000 x y output 有多少个起点可以走n(n>=0)步走到(x,y),只能从(x,y)走到(x,y+lcm(x,y))/(x+lcm(x,y) ...
- 【HDU 5382】 GCD?LCM! (数论、积性函数)
GCD?LCM! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 4497 GCD and LCM(数论+容斥原理)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm
题意: 给两个数,lll 和 ggg,为x , y , z,的最小公倍数和最大公约数,求出x , y , z 的值有多少种可能性 思路: 将x , y , z进行素因子分解 素因子的幂次 x a1 a ...
- hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...
- Mathematics:GCD & LCM Inverse(POJ 2429)
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
随机推荐
- PHP之数组array
$switching = array( 10, // key = 0 5 => 6, 3 ...
- 10道javascript笔试题
1.考察this JavaScript var length = 10; function fn() { console.log(this.length); } var obj = { length: ...
- C#小小总结(面向对象)
前言 学c#也有一年的时间了 以前零零散散的记的一些笔记啊 随便之类的 没有写过比较整体一点的总结 所以现在写一个小小的总结 内容 一.面向对象 相信刚开始接触编程的童鞋都被这个概念弄糊涂过,对于刚刚 ...
- span标签设置margin-top没有效果
<span>是行内元素,span只有margin-left和margin-right才有效果.要想margin-top生效就要把span转给块级元素才行.在span的css中加入以下属性即 ...
- mysql my.cnf 配置详解
#配置多实例声明[mysqld_multi]mysqld = /data/mysql_bin/bin/mysqld_safemysqladmin = /data/mysql_bin/bin/mysql ...
- 2015.10.15class
#include<stdio.h> main() { int a; printf("你的智商是多少?\n"); scanf("%d",&a) ...
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...
- css中的大小、定位、轮廓相关属性
css中的大小.定位.轮廓相关属性 1.通过height.width属性控制组件大小 height:高度,可以设置任何有效的距离值: width:宽度,可以设置任何有效的属性值: max-height ...
- eclipse中的窗口切换快捷键
Ctrl+Shift+F6 很简单,如果以后改用IDEA的话就没用了,但这个窗口切换确实很复杂,看起来也操作比较快.
- PetaPoco 批量插入数据
网上找的代码,还没经过验证 /// <summary> /// Bulk inserts multiple rows to SQL /// </summary> /// < ...