题目链接:hdu 4983 Goffi and GCD

题目大意:求有多少对元组满足题目中的公式。

解题思路:

  • n = 1或者k=2时:答案为1
  • k > 2时:答案为0(n≠1)
  • k = 1时:须要计算,枚举n的因子。令因子k=gcd(n−a,n,
    那么还有一边的gcd(n−b,n)=nk才干满足相乘等n。满足k=gcd(n−a,n)的a的个数即为ϕ(n/s),欧拉有o(n‾‾√的算法
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std;
const int maxn = 1e5;
const int MOD = 1e9+7;
typedef long long ll; ll ans;
int N, K, M;
int np, pri[maxn+5], vis[maxn+5];
int nf, fact[maxn+5], coun[maxn+5]; void prime_table (int n) {
np = 0;
memset(vis, 0, sizeof(vis)); for (int i = 2; i <= n; i++) {
if (vis[i])
continue;
pri[np++] = i;
for (int j = 2 * i; j <= n; j += i)
vis[j] = 1;
}
} void div_factor(int n) {
nf = 0;
for (int i = 0; i < np; i++) {
if (n % pri[i] == 0) {
coun[nf] = 0;
while (n % pri[i] == 0) {
coun[nf]++;
n /= pri[i];
}
fact[nf++] = pri[i];
}
} if (n != 1) {
coun[nf] = 1;
fact[nf++] = n;
}
} int euler_phi(int n) {
int m = (int)sqrt((double)n+0.5);
int ret = n;
for (int i = 2; i <= m; i++) {
if (n % i == 0) {
ret = ret / i * (i-1);
while (n%i==0)
n /= i;
}
} if (n > 1)
ret = ret / n * (n - 1);
return ret;
} ll add (int s) {
ll a = euler_phi(N/s);
ll b = euler_phi(s);
ll ret = a * b * 2; if (s == N / s)
ret /= 2;
return ret;
} void dfs (int s, int d) {
if (s > M)
return; if (d == nf) {
ans = (ans + add(s)) % MOD;
return;
} for (int i = 0; i <= coun[d]; i++) {
dfs(s, d+1);
s *= fact[d];
}
} int solve () {
ans = 0;
M = (int)sqrt((double)N);
div_factor(N);
dfs (1, 0);
return ans % MOD;
} int main () {
prime_table(maxn);
while (scanf("%d%d", &N, &K) == 2) {
if (N == 1)
printf("1\n");
else if (K > 2)
printf("0\n");
else if (K == 2)
printf("1\n");
else
printf("%d\n", solve());
}
return 0;
}

hdu 4983 Goffi and GCD(数论)的更多相关文章

  1. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

  2. hdu 4983 Goffi and GCD(欧拉函数)

    Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...

  3. HDU 4983 Goffi and GCD

    题目大意:给你N和K,问有多少个数对满足gcd(N-A,N)*gcd(N-B,N)=N^K.题解:由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N ...

  4. 【HDOJ】4983 Goffi and GCD

    题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为 ...

  5. HDU 4981 Goffi and Median(水)

    HDU 4981 Goffi and Median 思路:排序就能够得到中间数.然后总和和中间数*n比較一下就可以 代码: #include <cstdio> #include <c ...

  6. HDU 4982 Goffi and Squary Partition(推理)

    HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...

  7. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  8. hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)

    CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 2 ...

  9. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

随机推荐

  1. poj2485(Kruskal)

    这道题显然是一道最小生成树的问题,参考算法导论中的Kruskal方法,先对路径长度进行排序,然后使用并查集(Disjoint Set Union)来判断节点是否连通,记录连接所有节点的最后一条路径的长 ...

  2. Scrapy实战篇(四)之周杰伦到底唱了啥

    从小到大,一直很喜欢听周杰伦唱的歌,可是相信很多人和我一样,并不能完全听明白歌词究竟是什么,今天我们就来研究一下周董最喜欢在歌词中用的词,这一小节的构思是这样的,我们爬取周杰伦的歌词信息,并且将其进行 ...

  3. bzoj 3931: [CQOI2015]网络吞吐量 -- 最短路+网络流

    3931: [CQOI2015]网络吞吐量 Time Limit: 10 Sec  Memory Limit: 512 MB Description 路由是指通过计算机网络把信息从源地址传输到目的地址 ...

  4. JSON 与 JS 对象的关系

    很多人搞不清楚 JSON 和 Js 对象的关系,甚至连谁是谁都不清楚.简单来说: JSON 是 JS 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串. 如 var obj ...

  5. 05-树6. Path in a Heap (25) 小根堆

    05-树6. Path in a Heap (25) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.patest.cn/contes ...

  6. MySQL之thread cache

    最近突然对MySQL的连接非常感兴趣,从status根据thread关键字可以查出如下是个状态 show global status like 'thread%'; +---------------- ...

  7. 如何监控ActiveMQ

    如何监控ActiveMQ在ActiveMQ 4.x中你可以监控代理并查看使用了哪些目标,和连接一起的活动和订阅使用下面的工具进行查看: JMX和JMX控制台,如jConsole http://java ...

  8. Linux下Shell命令的输出信息同时显示在屏幕和保存到日志文件中

    #直接覆盖日志文件 ls -l | tee ./t.log #将输出内容附加到日志文件 ls -l | tee -a ./t.log 使用的是tee命令

  9. Ubuntu 16.09开启iptables的日志实现调试

    1.先配置日志文件输出 参考:http://www.cnblogs.com/EasonJim/p/8413535.html 2.配置日志打点 参考:http://www.cnblogs.com/Eas ...

  10. Ext文本输入框:Ext.form.TextField属性汇总(转) (

    本章介绍Ext.form.TextField组件的基本用法: <form id="form1" runat="server">    <div ...