题目链接:

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. Spring IOC源码分析之-刷新前的准备工作

    目录 ClassPathXmlApplicationContext的注册方式 加载父子容器 配置路径解析 容器刷新 刷新容器之刷新预处理 ClassPathXmlApplicationContext的 ...

  2. 移动app性能测试(待完善)

    移动终端性能测试是测试手机终端是否符合特定性能指标的测试,包括有:内存.CPU.电量.流量.流畅度.时延等 测试准备:测试账号.测试需求.测试用例.待测手机.待测应用包.测试工具.测试电脑 1.  时 ...

  3. Nonblocking Memory Refresh&2018ISCA/Security& 非阻塞内存刷新

    Abstract 我们提议的非阻塞刷新工作是一次刷新内存块中的一部分数据,并在内存块中使用冗余数据,如RS码,在块中计算块的刷新/不可读数据以满足读取请求.作为概念的证明,我们将非阻塞刷新应用于服务器 ...

  4. MongoDB的选举过程(转)

    转自:http://www.mongoing.com/archives/295 MongoDB的复制集具有自动容忍部分节点宕机的功能,在复制集出现问题时时,会触发选举相关的过程,完成主从节点自动切换. ...

  5. andeoid硬件解码

    Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...

  6. ssh无密码登陆权威指南

    [0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...

  7. Linux系统调用及用户编程接口(API)

    系统调用 所谓系统调用是指操作系统提供给用户程序调用的一组"特殊"接口,用户程序能够通过这组"特殊"接口来获得操作系统内核提供的服务.比如用户能够通过进程控制相 ...

  8. Channel (digital image) 通道 色彩深度 Color_depth

    en.wikipedia.org/wiki/Channel_(digital_image) Color digital images are made of pixels, and pixels ar ...

  9. 关于String,StringBuffer与StringBuilder的区别

    String是字符串常量对象,对其进行改变时会相当影响效率,特别注意在循环中直接拼接字符串效率非常差. 如果你想改变字符串的值,更加推荐使用StringBuffer与StringBuilder两种可变 ...

  10. 使用 Spring 容器管理 Filter

    当我们用Filter时,往往需要使用一些辅助的service,在普通的java中,只要声明(set,get方法)后在spring-application配置文件中配置就可以了,但是由于Filter与L ...