题目链接:

gcd

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 131072/131072 K (Java/Others)

Problem Description
 
Little White learned the greatest common divisor, so she plan to solve a problem: given x, n,
query ∑gcd(xa−1,xb−1) (1≤a,b≤n)
 
Input
 
The first line of input is an integer T ( 1≤T≤300)
For each test case ,the single line contains two integers x and n ( 1≤x,n≤1000000)
 
Output
 
For each testcase, output a line, the answer mod 1000000007
 
Sample Input
 
5
3 1
4 2
8 7
10 5
10 8
 
Sample Output
 
2
24
2398375
111465
111134466
 
题意:
 
求这个式子的值;
 
思路:
 
完全懵逼;看了题解才知道gcd(xa-1,xb-1)=xgcd(a,b)-1;好像以前在哪看过这个式子;
然后就变成了喜闻乐见的求和式子了.∑∑xgcd(a,b)-1;跟欧拉函数联系起来啦num[i]={gcd(a,b)==i的对数1<=a,b<=n}={gcd(a,b)==1的对数1<=a,b<=n/i}
欧拉函数啊;num[i]=2*{phi[1]+phi[2]+...+phi[n/i]}-1;这就是求∑num[i]*(xi-1)的和;再遍历一遍求答案还会超时;题解说要按n/i的值分成等比数列再求;就像那个约瑟夫变形问题按商分成求等差数列和一样;那就分层求好了,快速幂求逆,注意x==1的情况,最最重要的是要得到第一个那个公式;
 
AC代码:
 
/************************************************
┆ ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓   ┏━┛ ┆
┆  ┃   ┃  ┆      
┆  ┃   ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃    ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e6+10;
const int maxn=1e5+4;
const double eps=1e-8; int phi[N];
LL sum[N]; inline void Init()
{
phi[1]=1;
sum[1]=1;
For(i,2,N-1)
{
if(!phi[i])
{
for(int j=i;j<N;j+=i)
{
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
sum[i]=sum[i-1]+phi[i];
}
}
LL pow_mod(LL x,int y)
{
LL s=1,base=x;
while(y)
{
if(y&1)s=s*base%mod;
base=base*base%mod;
y>>=1;
}
return s;
} int main()
{
Init();
int t,n;
LL x;
read(t);
while(t--)
{
read(x);read(n);
LL ans=0;
if(x==1)cout<<"0\n";
else
{
LL temp=pow_mod(x-1,(int)mod-2);
int l=1,r;
while(l<=n)
{
r=n/(n/l);
LL g=((pow_mod(x,r+1)-pow_mod(x,l)+mod)*temp-(r-l+1)+mod)%mod;
ans=(ans+(2*sum[n/l]-1)%mod*g)%mod;
l=r+1;
}
cout<<ans<<"\n"; } }
return 0;
}

  

 

hdu-5780 gcd(数学)的更多相关文章

  1. 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$. ...

  2. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. HDU 5902 GCD is Funny 数学

    GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a ne ...

  4. 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 ...

  5. HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)

    题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...

  6. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  7. 数学--数论--HDU 5223 - GCD

    Describtion In mathematics, the greatest common divisor (gcd) of two or more integers, when at least ...

  8. 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)

    Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...

  9. HDU 4497 GCD and LCM (数学,质数分解)

    题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...

  10. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. js:argument

    引用:http://www.cnblogs.com/lwbqqyumidi/archive/2012/12/03/2799833.html    http://www.cnblogs.com/Fskj ...

  2. Swift初窥----语法进阶

    缺省绑定(Optional Binding 自己主动置空) 通过在类型变量后,加上?,能够实现缺省绑定为nil var window: UIWindow? 就是说,假设不正确window赋值,则win ...

  3. VMware-Fusion-7.0.0-2103067 Pro SN:序列号+ 百度云下载地址

    VMware-Fusion-7.0.0-2103067Pro SN: 5CQE9-H5PY3-04ND5-4Z6EW-3QGDE JZCNC-2H9X9-44TD9-Y0X5W-2KGP5 8ZNTC ...

  4. 企业级API设计

    最近对service的API设计,在team内有些讨论,主要集中在API是足够抽象.通用好呢, 还是具体.易用好? 其实这个是要折衷的,通用的好处是以后更改API的可能性小,但坏处是想要通用,里面的字 ...

  5. Rate Monotonic Scheduling algorithm

    这篇文章写得不错 http://barrgroup.com/embedded-systems/How-To/RMA-Rate-Monotonic-Algorithm 另外rtems的官方文档也有类似说 ...

  6. HTML中Select的使用详解

    <html><head><SCRIPT LANGUAGE="JavaScript"><!--//oSelect 列表的底部添加了一个新选项 ...

  7. CentOS搭建git服务器实测

    Git 可以使用四种主要的协议来传输数据:本地传输,SSH 协议,Git 协议和 HTTP 协议 1,安装: CentOS/Fedora: yum install git Ubuntu/Debian: ...

  8. android 底部菜单栏实现(转)

    1.Android学习之BottomNavigationBar实现Android特色底部导航栏 2.Android底部导航栏的四种实现 3.Android BottomNavigationBar底部导 ...

  9. JavaWeb学习总结第四篇--Servlet开发

    Servlet开发 用户在浏览器中输入一个网址并回车,浏览器会向服务器发送一个HTTP请求.服务器端程序接受这个请求,并对请求进行处理,然后发送一个回应.浏览器收到回应,再把回应的内容显示出来.这种请 ...

  10. Python学习总结之五 -- 入门函数式编程

    函数式编程 最近对Python的学习有些怠慢,最近的学习态度和学习效率确实很不好,目前这种病况正在好转. 今天,我把之前学过的Python中函数式编程简单总结一下,分享给大家,也欢迎并感谢大家提出意见 ...