除法取模与逆元--hdu3970 Harmonious Set
来源:互联网 发布:微信清理后的数据恢复 编辑:程序博客网 时间:2024/06/18 11:52
。。。虽然不知道是怎么推出来的,答案是
a(n) = 1/n * sum_{d divides n and d is odd}(2^(n/d) * phi(d)) phi : 欧拉函数
答案对1e9 + 7取模。
1.除法取模与逆元
(a / b) % mod = (a * 1 / b) % mod
设(a * 1 / b) % mod = (a * bs) % mod,bs是1 / b对于%mod运算的逆元。
所以(1 / b == bs) % mod
(bs * b == 1) %mod
bs * b + k * mod == 1
x * b + y * mod == 1可用扩展gcd求。得出的x即为bs,b对于%mod的逆元。
当b和mod不互质,即d不等于1时,y对于%mod的逆元不存在
2.范围是1e9不能打表求欧拉函数,应该直接求。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespacestd;
const int mod =1000000007;
typedef longlong ll;
int phi(int x)
{
int res = x;
for (int i =2; i <=sqrt(x); i ++) {
if(x % i ==0) {
res = res / i * (i - 1);
while (x % i ==0) x /= i;//保证i是质数
}
}
if(x >1) res = res / x * (x -1);//最后剩下一个质数
return res;
}
ll quick_pow(int a,int b)
{
ll res =1,k =1ll * a;
while (b) {
if(b &1) res = (res %mod * k %mod) %mod;
k = (k % mod * k %mod) %mod;
b >>= 1;
}
return res;
}
void ext_gcd(int a,int b,ll &d,ll &x,ll &y)
{
if(b ==0){
x = 1;y =0;d = a;
return;
}
ext_gcd(b,a % b,d,y,x);
y -= x * (a / b);
}
ll getbs(int n)//求逆元
{
ll bs,y,d;
ext_gcd(n,mod, d, bs, y);
return bs;
}
void solve(int n)
{
ll sum =0;
for (int i =1; i <=sqrt(n); i ++) {
if(n % i ==0)
{
if(i %2) sum = (sum +quick_pow(2, n / i) *phi(i)) %mod;
if(i != n / i && (n / i) %2) sum = (sum +quick_pow(2, i) *phi(n / i)) %mod;
}
}
printf("%lld\n",(mod + (sum %mod * getbs(n) %mod) %mod )%mod);
}
int main()
{
int C;
cin >> C;
int n;
while (C --) {
scanf("%d",&n);
solve(n);
}
return0;
}
- 除法取模与逆元--hdu3970 Harmonious Set
- 除法取模与逆元
- 除法取模与逆元
- 除法取模与逆元
- 除法取模与逆元/费马小定理
- 除法取模与逆元/费马小定理
- 乘法逆元(除法取模)
- 除法取模(逆元)
- 除法取模 逆元 费马小定理
- 除法取模和逆元
- 小知♂识 除法取模 逆元♂
- hdu 5407 CRB and Candies(素数筛选法,除法取模(乘法逆元))
- HDU 5690 查找循环节 数学公式快速幂+乘法逆元(除法取模)
- 基础数论学习笔记-----------逆元【除法取模运算的关键】
- Kickstart Round A 2017 Problem A. Square Counting 公式、数论逆元、除法取模
- 除法运算、商、余数与取模
- 移位运算与除法、取模运算
- 取余与除法
- Ubuntu 16.04搭建LAMP开发环境
- 爬虫自学笔记(Python3.6.1)
- Comparator<String>接口,String类数组按字符串长度排序
- linux命令--rpm命令
- [DP] POJ
- 除法取模与逆元--hdu3970 Harmonious Set
- 悦读---《读者》(2)
- 用递归实现字符串的逆置
- jsp简单介绍
- 【思维-桶记录数组动态滚动】Encrypted Password UVALive
- git详细教程
- HDU —5120 Intersection
- 414. Third Maximum Number
- UVA11552[Fewest Flops] 动态规划