题目链接:

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. WCF TCP通信方式 通过IIS承载调试

    http://www.cnblogs.com/nikymaco/archive/2012/10/08/2715954.html IIS Express服务器只支持http/https,不支持net.t ...

  2. 京东2015年应届生招聘笔试题(A)卷答案选择题部分

    1.操作系统死锁的必要条件(多选题):()   A.相互排斥条件   B.系统资源有限   C.进程调度不合理   D.环路等待条件 答:A,C,D 參考资料:http://blog.sina.com ...

  3. vim 穿越时空

    1. 回到以前的文件状态 :earlier 3m   回到文件3分钟之前的状态 2. 回到以后的文件状态 :later 3m      回到文件3分钟之后的状态 3. 时间单位 s 秒 m 分钟 d ...

  4. webpack3.0 环境搭建

    额.备份一下总是好的 #为了避免某些国外镜像源安装失败,先设置淘宝镜像代理 yarn config set registry https://registry.npm.taobao.org # 初始化 ...

  5. The Google File System论文拜读

    The Google File System Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung Google∗ 摘要 我们设计并实现了谷歌文件系统 ...

  6. HDU3549_Flow Problem(网络流/EK)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  7. Flyweight Design Pattern 共享元设计模式

    就是利用一个类来完毕多种任务.不用每次都创建一个新类. 个人认为这个设计模式在C++里面,好像能够就使用一个函数取代,利用重复调用这个函数完毕任务和重复利用这个类,好像几乎相同. 只是既然是一个设计模 ...

  8. Mybatis之Mapper动态代理

    一.什么是Mapper的动态代理 采用Mapper动态代理方法只需要编写相应的Mapper接口(相当于Dao接口),那么Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接 ...

  9. linux自动ftp上传与下载文件的简单脚本

    #!/bin/sh cd /data/backup/55mysql DATE=`date +'%Y%m%d'`file="55_mysql_"$DATE"03*.rar& ...

  10. ast.literal_eval(jsonStr) json格式字符串转dict 2种方式

    json.loads(jsonStr, encoding='utf-8')import ast ast.literal_eval(jsonStr)