HDU 4983 Goffi and GCD(数论)
HDU 4983 Goffi and GCD
思路:数论题。假设k为2和n为1。那么仅仅可能1种。其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了。k = 1的时候,枚举n的因子,然后等于求该因子满足的个数,那么gcd(x, n) = 该因子的个数为phi(n / 该因子),然后再利用乘法原理计算就可以
代码:
#include <cstdio>
#include <cstring>
#include <cmath> typedef long long ll; const ll MOD = 1000000007;
const int N = 35333; ll n, k, pn, vis[N];
ll prime[N], frc[N], fn, cnt[N]; void getprime() {
pn = 0;
for (ll i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (ll j = i * i; j < N; j += i)
vis[j] = 1;
}
} void getfrc(ll n) {
fn = 0;
for (ll i = 0; i < pn && n >= prime[i]; i++) {
if (n % prime[i] == 0) {
frc[fn] = prime[i];
cnt[fn] = 0;
while (n % prime[i] == 0) {
cnt[fn]++;
n /= prime[i];
}
fn++;
}
}
if (n != 1) {
frc[fn] = n;
cnt[fn++] = 1;
}
} ll ans = 0; ll phi(ll n) {
ll m = (ll)sqrt(n * 1.0);
ll ans = n;
for (ll i = 2; i <= m; i++) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
}
if (n > 1) ans = ans / n * (n - 1);
return ans;
} void dfs(ll u, ll sum) {
if (u == fn) {
ll r = n / sum;
ans = (phi(n / sum) * phi(sum) % MOD + ans) % MOD;
return;
}
for (ll i = 0; i <= cnt[u]; i++) {
dfs(u + 1, sum);
sum *= frc[u];
}
} ll solve() {
getfrc(n);
ans = 0;
dfs(0, 1);
return ans;
} int main() {
getprime();
while (~scanf("%I64d%I64d", &n, &k)) {
if (n == 1) printf("1\n");
else if (k == 2) printf("1\n");
else if (k > 2) printf("0\n");
else {
printf("%I64d\n", solve());
}
}
return 0;
}
HDU 4983 Goffi and GCD(数论)的更多相关文章
- hdu 4983 Goffi and GCD(数论)
题目链接:hdu 4983 Goffi and GCD 题目大意:求有多少对元组满足题目中的公式. 解题思路: n = 1或者k=2时:答案为1 k > 2时:答案为0(n≠1) k = 1时: ...
- hdu 4983 Goffi and GCD(欧拉函数)
Problem Description Goffi is doing his math homework and he finds an equality on his text book: gcd( ...
- 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 ...
- 【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不为 ...
- HDU 4981 Goffi and Median(水)
HDU 4981 Goffi and Median 思路:排序就能够得到中间数.然后总和和中间数*n比較一下就可以 代码: #include <cstdio> #include <c ...
- HDU 4982 Goffi and Squary Partition(推理)
HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 5656 CA Loves GCD(n个任选k个的最大公约数和)
CA Loves GCD Accepts: 64 Submissions: 535 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
随机推荐
- 微信小程序中的图形验证码
可以在utils中新建一个mcaptcha.js 代码如下: module.exports = class Mcaptcha { constructor(options) { this.options ...
- org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
---恢复内容开始--- /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache ...
- spring mvc介绍只试图解析(转载)
转载路径 http://haohaoxuexi.iteye.com/blog/1770554 SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这 ...
- 解决【npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141072930277'】方案
问题描述执行npm install的时候报错npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141 ...
- Jupyter IPython dead kernel and do not restart
本人遇到的情况:dead kernel & try to restart failed 查看CMD发现这个库安装有问题 解决办法 1.pip uninstall backports.shuti ...
- Vue-cli 3.0自定义脚手架
一.进入项目地址 https://github.com/vuejs/vue-cli ,选择 docs目录查看具体安装流程. 中文文档:https://cli.vuejs.org/zh 可以看到我电脑上 ...
- Linux 关于umount
场景:linux下挂载过去的代码目录编译失败.怀疑本地磁盘空间不足问题导致.解决方法:卸载重新挂载. 操作:卸载时报错: 解决方法: 1.umount, 老是提示:device is busy, 服务 ...
- centos 简单用户管理
一.配置文件 /etc/passwd:存放用户信息,以“:”分割成7个部分 1.账号名称,用来对应UID: 2.早期密码存放位置,后来密码改存/etc/shadow中,以“x”代替: 3.UID,使用 ...
- PHP:车牌照合法性
文章来源:http://www.cnblogs.com/hello-tl/p/7592362.html <?php /** * [verifyCarCard description] 效验车牌号 ...
- [转] angular2-highcharts用法详解
1. 使用npm安装angular2-highcharts npm install angular2-highcharts --save 2.主模块中引入 app.module.ts import { ...