Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N. 
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.

·······

这道题分几步:

1、先把n用算术基本原理拆分成不同的质数,如果a~b中的数是这些质数的倍数那其肯定不与n互质,a~b的总个数减去不与n互质的数剩下来就是与n互质的数啦

2、先计算1~a-1中与n互质的个数,再计算1~b中与n互质的个数,相减。

#include<iostream>
using namespace std;
typedef long long ll;
ll a[];
int main()
{
ll t;
cin>>t;
for(ll ii=;ii<=t;ii++)
{
ll x,y,n;
cin>>x>>y>>n;
ll num=,sum=,all=,times;
for(int i=;i*i<=n;i++) //向a数组里塞n的质因数
{
if(n%i==)
{
num++;
a[num]=i;
}
while(n%i==)
{
n=n/i;
}
}
if(n>) a[++num]=n;
for(int i=;i<(<<num);i++)
{
times=,sum=;
for(int j=;j<num;j++) //num个因数,乘了几个
{
if(&(i>>j)) //判断有没有选中第j个因数
{
sum*=a[j+];
times++;
}
}
if(sum==||sum==)
continue;
if(times%==)
{
all+=y/sum;
all-=(x-)/sum;
}
else{
all-=y/sum;
all+=(x-)/sum;
}
}
printf("Case #%lld: %lld\n",ii,y-x+-all);
}
return ;
}

以上。

C - Co-prime的更多相关文章

  1. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  2. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  5. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  6. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  7. 最小生成树 prime zoj1586

    题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...

  8. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  9. 最小生成树 prime + 队列优化

    存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...

  10. 最小生成树 prime poj1287

    poj1287 裸最小生成树 代码 #include "map" #include "queue" #include "math.h" #i ...

随机推荐

  1. laravel5.4的laravel-mix踩坑记

    第一个坑,cross-env目录不对 sh: node_modules/cross-env/bin/cross-env.js: No such file or directory 我查了一下cross ...

  2. Grafana +Zabbix 系列二

    Grafana +Zabbix 系列二 Grafana 简介补充 Grafana自身并不存储数据,数据从其他地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化图形 ...

  3. getFieldDecorator用法(二)——封装表单模块

    后台管理系统经常用到表单查询,因此做了个封装 myorder.js import React from 'react'; import { Card, Button, Table, Form, Sel ...

  4. pwn学习日记Day10 《程序员自我修养》读书笔记

    第一章 从 Hello world 说起 抛出问题: 1.程序为什么要被编译器编译后才能执行? 2.编译器在把C语言程序转换成可以执行的机器码的过程中做了什么,怎么做的? 3.最后编译出来的可执行文件 ...

  5. OpenResty之 lua_shared_dict 指令

    1. lua_shared_dict 指令介绍 原文: lua_shared_dict syntax:lua_shared_dict <name> <size> default ...

  6. spring boot 学习常用网站

    springboot的特性 https://www.cnblogs.com/softidea/p/5644750.html 1.自定义banner https://www.cnblogs.com/cc ...

  7. ionic serve 报【ionic-app-scripts' 不是内部或外部命令 】问题解

    Error: Cannot find module 'E:\angular\ionic0515\node_modules\@ionic\app-scripts'    at Function.Modu ...

  8. HearthBuddy Ai 调试实战2 在使用海巨人的时候,少召唤了一个图腾(费用是对的)

    问题 游戏面板 8是青玉之爪13是海巨人17是恐狼前锋 64是萨满 66是圣骑士63,99,46,是微型木乃伊[其中99和46都是2血3攻,63是2血1攻]57是鱼人木乃伊 微型木乃伊 "L ...

  9. Python 今天抽空学习了@Property

    1.@Property有啥用 1) @property使方法像属性一样调用 @property可以把一个实例方法变成其同名属性,以支持.号访问,它亦可标记设置限制,加以规范 2) @property成 ...

  10. 前端中关于HTML标签的属性for的理解

    First:<label>的说明:1.<label>标签为input元素定义标注(标识)2.label元素不会像用户呈现任何特殊的效果,仅作为显示扩展:不过,它为鼠标用户改进了 ...