hdu 5780 gcd
题意:给定$x, n$满足$1 \leq x, n \leq 1000000$,求$\sum{(x^a-1,x^b-1)}$对$1e9+7$取模后的值,其中$1 \leq a, b \leq n$。
分析:首先不难有$(x^a - 1, x ^ b - 1) = x^{(a,b)}-1$(证明方法可沿欧几里得定理思路),那么我们只需要考虑$(a,b) = d$即可,设$f(d)$为使得$(a, b) = d$的对数,那么不难有$ans = \sum_{d = 1}^{n}{f(d)(x^d-1)}$。下面考虑计算$f(d)$,由于$(a, b) = d$满足$d \mid a, d \mid b, (\frac{a}{d}, \frac{b}{d}) = 1$,于是对于满足$1\leq a \leq b \leq n$的序对$(a, b)$,其总数为$\sum_{i=1}^{\frac{n}{d}}{\varphi(i)}$,因为我们可以在互质的对上乘以系数$d$使其映射到对$(a, b)$。那么对于题目中的要求,总数为$2\sum_{i=1}^{\frac{n}{d}}{\varphi(i)}-1$,那么我们就只用到了欧拉函数的前缀和,注意到$f(d)$只与$\frac{n}{d}$有关,而右边可以使用等比求和,于是可以分段计算,每段对应的$\frac{n}{d}$固定。那么这样做预处理是$O(n)$的,每次询问的复杂度是$O(\sqrt{n})$的。代码如下:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <cmath>
#include <iostream>
#include <assert.h>
#define PI acos(-1.)
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp std :: make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii std :: pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) std::cout << x << std::endl
#define dbg2(x, y) std::cout << x << " " << y << std::endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << ) - );
const double double_inf = 1e30;
const double eps = 1e-;
typedef unsigned long long ul;
typedef unsigned int ui;
inline int readint(){
int x;
scanf("%d", &x);
return x;
}
inline int readstr(char *s){
scanf("%s", s);
return strlen(s);
}
//Here goes 2d geometry templates
struct Point{
double x, y;
Point(double x = , double y = ) : x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B){
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x){
if(abs(x) < eps) return ;
return x < ? - : ;
}
bool operator == (const Point& a, const Point& b){
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
}
double Dot(Vector A, Vector B){
return A.x * B.x + A.y * B.y;
}
double Len(Vector A){
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B){
return acos(Dot(A, B) / Len(A) / Len(B));
}
double Cross(Vector A, Vector B){
return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C){
return Cross(B - A, C - A);
}
Vector Rotate(Vector A, double rad){
//rotate counterclockwise
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){
double L = Len(A);
return Vector(-A.y / L, A.x / L);
}
void Normallize(Vector &A){
double L = Len(A);
A.x /= L, A.y /= L;
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
double DistanceToLine(Point P, Point A, Point B){
Vector v1 = B - A, v2 = P - A;
return abs(Cross(v1, v2)) / Len(v1);
}
double DistanceToSegment(Point P, Point A, Point B){
if(A == B) return Len(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < ) return Len(v2);
else if(dcmp(Dot(v1, v3)) > ) return Len(v3);
else return abs(Cross(v1, v2)) / Len(v1);
}
Point GetLineProjection(Point P, Point A, Point B){
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
//Line1:(a1, a2) Line2:(b1,b2)
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
}
bool OnSegment(Point p, Point a1, Point a2){
return dcmp(Cross(a1 - p, a2 - p)) == && dcmp(Dot(a1 - p, a2 -p)) < ;
}
Vector GetBisector(Vector v, Vector w){
Normallize(v), Normallize(w);
return Vector((v.x + w.x) / , (v.y + w.y) / );
} bool OnLine(Point p, Point a1, Point a2){
Vector v1 = p - a1, v2 = a2 - a1;
double tem = Cross(v1, v2);
return dcmp(tem) == ;
}
struct Line{
Point p;
Vector v;
Point point(double t){
return Point(p.x + t * v.x, p.y + t * v.y);
}
Line(Point p, Vector v) : p(p), v(v) {}
};
struct Circle{
Point c;
double r;
Circle(Point c, double r) : c(c), r(r) {}
Circle(int x, int y, int _r){
c = Point(x, y);
r = _r;
}
Point point(double a){
return Point(c.x + cos(a) * r, c.y + sin(a) * r);
}
};
int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, std :: vector<Point>& sol){
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
double e = a * a + c * c, f = * (a * b + c * d), g = b * b + d * d - C.r * C.r;
double delta = f * f - * e * g;
if(dcmp(delta) < ) return ;
if(dcmp(delta) == ){
t1 = t2 = -f / ( * e); sol.pb(L.point(t1));
return ;
}
t1 = (-f - sqrt(delta)) / ( * e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / ( * e); sol.pb(L.point(t2));
return ;
}
double angle(Vector v){
return atan2(v.y, v.x);
//(-pi, pi]
}
int GetCircleCircleIntersection(Circle C1, Circle C2, std :: vector<Point>& sol){
double d = Len(C1.c - C2.c);
if(dcmp(d) == ){
if(dcmp(C1.r - C2.r) == ) return -; //two circle duplicates
return ; //two circles share identical center
}
if(dcmp(C1.r + C2.r - d) < ) return ; //too close
if(dcmp(abs(C1.r - C2.r) - d) > ) return ; //too far away
double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / ( * C1.r * d));
Point p1 = C1.point(a - da), p2 = C1.point(a + da);
sol.pb(p1);
if(p1 == p2) return ;
sol.pb(p2);
return ;
}
int GetPointCircleTangents(Point p, Circle C, Vector* v){
Vector u = C.c - p;
double dist = Len(u);
if(dist < C.r) return ;//p is inside the circle, no tangents
else if(dcmp(dist - C.r) == ){
// p is on the circles, one tangent only
v[] = Rotate(u, PI / );
return ;
}else{
double ang = asin(C.r / dist);
v[] = Rotate(u, -ang);
v[] = Rotate(u, +ang);
return ;
}
}
int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
//a[i] store point of tangency on Circle A of tangent i
//b[i] store point of tangency on Circle B of tangent i
//six conditions is in consideration
int cnt = ;
if(A.r < B.r) { std :: swap(A, B); std :: swap(a, b); }
int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
int rdiff = A.r - B.r;
int rsum = A.r + B.r;
if(d2 < rdiff * rdiff) return ; // one circle is inside the other
double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
if(d2 == && A.r == B.r) return -; // two circle duplicates
if(d2 == rdiff * rdiff){ // internal tangency
a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
return ;
}
double ang = acos((A.r - B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
if(d2 == rsum * rsum){
//one internal tangent
a[cnt] = A.point(base);
b[cnt++] = B.point(base + PI);
}else if(d2 > rsum * rsum){
//two internal tangents
double ang = acos((A.r + B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
}
return cnt;
}
Point ReadPoint(){
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
}
Circle ReadCircle(){
double x, y, r;
scanf("%lf%lf%lf", &x, &y, &r);
return Circle(x, y, r);
}
//Here goes 3d geometry templates
struct Point3{
double x, y, z;
Point3(double x = , double y = , double z = ) : x(x), y(y), z(z) {}
};
typedef Point3 Vector3;
Vector3 operator + (Vector3 A, Vector3 B){
return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
}
Vector3 operator - (Vector3 A, Vector3 B){
return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
}
Vector3 operator * (Vector3 A, double p){
return Vector3(A.x * p, A.y * p, A.z * p);
}
Vector3 operator / (Vector3 A, double p){
return Vector3(A.x / p, A.y / p, A.z / p);
}
double Dot3(Vector3 A, Vector3 B){
return A.x * B.x + A.y * B.y + A.z * B.z;
}
double Len3(Vector3 A){
return sqrt(Dot3(A, A));
}
double Angle3(Vector3 A, Vector3 B){
return acos(Dot3(A, B) / Len3(A) / Len3(B));
}
double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
return abs(Dot3(p - p0, n));
}
Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
return p - n * Dot3(p - p0, n);
}
Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
Vector3 v = p2 - p1;
double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
return p1 + v * t;//if t in range [0, 1], intersection on segment
}
Vector3 Cross(Vector3 A, Vector3 B){
return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
}
double Area3(Point3 A, Point3 B, Point3 C){
return Len3(Cross(B - A, C - A));
}
class cmpt{
public:
bool operator () (const int &x, const int &y) const{
return x > y;
}
}; int Rand(int x, int o){
//if o set, return [1, x], else return [0, x - 1]
if(!x) return ;
int tem = (int)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
}
void data_gen(){
srand(time());
freopen("in.txt", "w", stdout);
int kases = ;
printf("%d\n", kases);
while(kases--){
int sz = 2e4;
int m = 1e5;
printf("%d %d\n", sz, m);
FOR(i, , sz) printf("%d ", Rand(, ));
printf("\n");
FOR(i, , sz) printf("%d ", Rand(1e9, ));
printf("\n");
FOR(i, , m){
int l = Rand(sz, );
int r = Rand(sz, );
int c = Rand(1e9, );
printf("%d %d %d %d\n", l, r, c, Rand(, ));
}
}
} struct cmpx{
bool operator () (int x, int y) { return x > y; }
};
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
ll phi[maxn];
void init_miu(){
clr(phi, );
phi[] = ;
FOR(i, , maxn - ) if(!phi[i]){
for(int j = i; j < maxn; j += i){
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
FOR(i, , maxn - ) phi[i] = (phi[i] + phi[i - ]) % mod;
}
ll power(ll a, ll p, ll mod){
ll res = ;
a %= mod;
while(p){
if(p & ) res = res * a % mod;
p >>= ;
a = a * a % mod;
}
return res;
} ll cal2(int x, int n){
ll ans = ;
int p = ;
while(p <= n){
int np = n / (n / p);
int delta = np - p + ;
ll cnt = ( * phi[n / p] + mod - ) % mod;
ll lhs = ;
if(x > ){
lhs = power(x, p, mod) * (power(x, delta, mod) + mod - ) % mod;
lhs = lhs * power(x - , mod - , mod) % mod;
lhs = (lhs - delta + mod) % mod;
}
ans = (ans + lhs * cnt % mod) % mod;
p = np + ;
}
return ans;
}
int main(){
//data_gen(); return 0;
//C(); return 0;
int debug = ;
if(debug) freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
init_miu();
int T = readint();
while(T--){
int x = readint(), n = readint();
printf("%lld\n", cal2(x, n));
}
return ;
}
code:
当然本题我用莫比乌斯反演也勉强通过了(用了900多ms,仅仅通过了1次),使用了两次分段求和,每次询问的复杂度介于$O(\sqrt{n})$与$O(n)$之间。虽然不是正解,仍然可以看出欧拉函数与莫比乌斯函数之间关系相通。
hdu 5780 gcd的更多相关文章
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5902 GCD is Funny 数学
GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a ne ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- hdu 4497 GCD and LCM 数学
GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...
- HDU 4675 GCD of Sequence(容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给出n,m,K,一个长度为n的数列A(1<=A[i]<=m).对于d(1< ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
随机推荐
- css渐变颜色在线制作
http://www.colorzilla.com/gradient-editor/
- 文件上传插件 -- plupload
refresh:重新实例化uploader removeFile(id):从file中移除某个文件 splice(start,length):从队列中start开始删除length个文件, 返回被删除 ...
- 利用.htaccess实现伪静态方法
首先配置服务器启动重写模块打开 Apache 的配置文件 httpd.conf .将#LoadModule rewrite_module modules/mod_rewrite前面的#去掉.保存后重启 ...
- Android课程---表格布局TableLayout
特别注意:由于表格布局继承自线性布局,因此并不显示表格线 示例代码: <?xml version="1.0" encoding="utf-8"?> ...
- shader三种变量类型(uniform,attribute和varying)
uniform变量在vertex和fragment两者之间声明方式完全一样,则它可以在vertex和fragment共享使用.(相当于一个被vertex和fragment shader共享的全局变量) ...
- IE8兼容H5语义标签
//IE浏览器定义的特殊属性,通过hack方式判断IE版本来执行不同的代码,IE8以下浏览器自动创建html5语义标签,从而实现兼容<!--[if lte IE 8] <script sr ...
- IAR EW8051-8.10.4安装及破解方法
第一步:获取破解license 1: 点击桌面左下角“开始”按钮,找到cmd.exe,右键创建cmd.exe 快捷方式到桌面: ————如果是windows7 ,请右键点击cmd.exe 快捷图标,点 ...
- LINUX VI 常用命令
vi 打开或新建 vi filename 打开或新建文件 并将光标置于第一行首 光标 ) 光标移至句尾 ( 光标移至句首 屏幕翻滚类命令 Ctrl+u 向文件首翻半屏 Ctrl+d 向文件尾翻半屏 ...
- JavaScript 代码 优化笔记
1. 判断某个元素是否在数组中. setCheckNodes : function (zNodes, checkIds){ var that = this; that.setAllNodesUnche ...
- 如何真正抓住微信小程序的红利? 阿禅知乎live总结
微信App定义 为满足用户某种开发需求.完全基于微信的消息或网页应用,入口是公众号,用户无需离开微信即可完成所有操作,所有需求都在公众号里被满足 微信App的优势 1. 顾客在哪里,就让顾客在哪里看到 ...