URAL 1907. Coffee and Buns(数论推导+容斥原理)

1907. Coffee and Buns

Time limit: 1.0 second

Memory limit: 64 MB

Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervision from the early
age. Each education center is a small town with all the necessary utilities. During the construction of a center, a square area is chosen which is divided into equal sections each sized 100 × 100 meters. In each of these sections they build one building, which
would become residential or academic. The outer perimeter of the center is fenced.

After a successful military operation in the Andromeda nebula the active colonization of habitable planets has started. The need for people able to take command and lead people to the new worlds has
increased. Therefore, two new education centers should be built on Ataraxia. Discussion about the details of the project in the local administration is already underway for many days. During this time it was decided that the first center will consist of a2 sections
and that the second one will consist of no more than n2 sections. The situation is complicated because, according to requirements of the antimonopoly legislation, construction
works must be performed by at least two different companies, each of them must build an equal number of buildings and an equal number of 100-meters segments of the fence.

You are responsible for resupplying the administration office. You understand that while they are discussing the pros and cons of each possible size of the second center a lot of buns and coffee will
be consumed, and it‘s time to buy them. So you‘d like to know how many different sizes of the second center will meet the requirements of antimonopoly legislation and, therefore, will be fully considered by the administration.

Input

The only line contains integers a and n (1 ≤ a ≤ 1012; 1 ≤ n ≤ 1018).

Output

Output an amount of different sizes of the second center meeting the requirements of antimonopoly legislation.

Sample

input output
3 6
4

Notes

In this example it is possible to build the second center sized 3 × 3 or 6 × 6, delegating the construction to three different companies, or to build it sized 1 × 1 or 5 × 5, delegating the construction
to two different companies.

Problem Author: Ivan Burmistrov

Problem Source: Ural Championship 2012

Tags: none  (
hide tags for unsolved problems
)

Difficulty: 985    Printable version    Submit solution    Discussion
(0)

All submissions (654)    All accepted submissions (223)    Solutions
rating (123)

大致题意:

输入 a n 判断 从 b = 1 ~ n 中 满足 gcd( ( a * a + b * b ) , 4 * a + 4 * b ) > 1 的个数。

a < 1e12 , n < 1e18

若 a b 奇偶性相同,显然满足。gcd是2的倍数。

若a b  奇偶性不同,

开始推导:

因为 a * a + b * b 是奇数,

所以

gcd( a * a + b * b , 4 * a + 4 * b ) > 1 等价于

gcd( a * a + b * b , a + b ) > 1 等价于

根据整除性质,若存在 e > 1 ,满足 e | ( a + b ) , 那么 e | ( a + b ) ^ 2 反之亦然

则等价于

gcd( a * a + b * b , a * a + b * b + 2 * a * b ) > 1 等价于

gcd( 2 * a * a + 2 * b * b , a * a + b * b + 2 * a * b ) > 1等价于

gcd( a * a + b * b - 2 * a * b , a * a + b * b + 2 * a * b ) > 1 等价于

gcd( a - b , a + b ) > 1 等价于

gcd( 2 * a , a + b ) > 1

因为 a+b是奇数

等价于

gcd( a , a +b ) > 1等价于

gcd( a , b ) > 1

推导结束

下面对a分解质因数,找出n个数中不同奇偶性且满足gcd( a , b ) > 1的个数

最后加上 相同奇偶性的 个数 就是答案。

所以要求1~maxn中与a,gcd > 1 的个数,就是求1~maxn与某一个num不互素的个数,要用到容斥原理:

ll no_coprime(ll num,ll MAXN){//1~maxn与num不互素的个数
        ll ans = 0;
        vector<ll> fac;
        for(ll i = 2; i*i <= num; i++){ //分解因数
                if( num % i == 0){
                        fac.push_back(i);
                        while(num%i == 0) num /= i;
                }
        }
        if(num != 1) fac.push_back(num);
        int sz = SZ(fac);
        for(ll mask = 1 ; mask < (1LL<<sz); mask++){ //容斥过程,复杂度2^因数个数
                ll c = 0;
                ll tmp = 1;
                rep(i,sz) if( (1LL<<i)&mask){
                        c++;
                        tmp *= fac[i];
                }
                if(c&1) ans += MAXN/tmp;
                else ans -= MAXN/tmp;
        }
        return ans;
}

ac代码:

//Accepted 412	15 G++ 4.9 C++11 2092
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define rep(i,n) for ( int i=0; i<int(n); i++ )
using namespace std;
typedef long long ll;
#define X first
#define Y second
typedef pair<ll,ll> pii;

template <class T>
inline bool RD(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void PT(T x) {
	if (x < 0) {
		putchar('-');

		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}

ll a,n;

ll no_coprime(ll num,ll MAXN){
        ll ans = 0;
        vector<ll> fac;
        for(ll i = 2; i*i <= num; i++){ //分解因数
                if( num % i == 0){
                        fac.push_back(i);
                        while(num%i == 0) num /= i;
                }
        }
        if(num != 1) fac.push_back(num);
        int sz = SZ(fac);
        for(ll mask = 1 ; mask < (1LL<<sz); mask++){ //容斥过程,复杂度2^因数个数
                ll c = 0;
                ll tmp = 1;
                rep(i,sz) if( (1LL<<i)&mask){
                        c++;
                        tmp *= fac[i];
                }
                if(c&1) ans += MAXN/tmp;
                else ans -= MAXN/tmp;
        }
        return ans;
}

int main(){

        while(cin>>a>>n){
                ll ans = 0;
                if(a&1) ans += (n+1)/2, n /= 2;
                ans += no_coprime(a,n);
                printf("%lld\n",ans);
        }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-16 19:16:54

URAL 1907. Coffee and Buns(数论推导+容斥原理)的相关文章

ural 1907 Coffee and Buns

1907. Coffee and Buns Time limit: 1.0 secondMemory limit: 64 MB Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervisio

ural 1932 The Secret of Identifier (容斥原理)

题目大意: 求出给的n个串中. 精确到只有一个字符不同,两个字符不同,三个字符不同,四个字符不同的对数. 思路分析: 枚举状态. dp[i] [j] ...表示当前串取出 i 状态下的所有字符转化成十进制数为 j 的出现的次数. 这样的话,就记录了所有串的子串的状态. 然后计数就得到了所有的状态. 然后我们要得到精确不同的,可以用补集的思想,如果要精确到三个不相同,意味着要精确到1 个是相同的. 注意的问题是 在最后要运用容斥去重. #include <cstdio> #include <

【暑期集训第一场】欧拉回路 | 思维 | 数论构造 | 容斥原理 | 线段树 | 归并排序

集训1(HDU2018 Multi-University Training Contest 2) ID A B C D E F G H I J AC O O 补题 ? O ? O 代码 & 简易题解 [A]:期望? 神仙题,留坑.. [B]:?? 同\(\text{A}\) [C]:求欧拉通路条数,以及每条的路径 小学数竞里有讲过,无向图一笔画的充要条件是有零个或两个"奇点"(偶点个数不限),"奇点"在这里就是指度为奇数的点... 其实上面两种情况就分别对应

zoj.3868.GCD Expectation(数学推导&gt;&gt;容斥原理)

GCD Expectation Time Limit: 4 Seconds                                     Memory Limit: 262144 KB Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, x2,…,xm} (each nonempty subset has equal probability to be pick

URAL 1139. City Blocks (数论)

1139. City Blocks Time limit: 1.0 second Memory limit: 64 MB The blocks in the city of Fishburg are of square form. N avenues running south to north and M streets running east to west bound them. A helicopter took off in the most southwestern crossro

Codeforces Round #428 (Div. 2) D. Winter is here[数论II][容斥原理]

传送门:http://codeforces.com/contest/839/problem/D Examples input 33 3 1 output 12 input 42 3 4 6 output 39 Note In the first sample the clans are {1},?{2},?{1,?2} so the answer will be 1·3?+?1·3?+?2·3?=?12 题解:当有n个数为x的倍数时 gcd为x对答案的贡献为$1*C_n^1+2*C_n^2+..

Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)

传送门 解题思路: 假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是 假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束 那么根据组合数,我们仍然可以算出其不合法的解共有: 最后,由于根据容斥,减两遍的东西要加回来,那么含有偶数个 f 的项为正,奇数个时为负. 答案就是: 搜索答案,使用Lucas定理,计算组合数上下约去. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include&

部分系列题

当然是不齐的. JZP系列 JZPKIL       数论,反演,积性函数,伯努利数,(常数优化) JZPFAR     k-d树 JZPTAB    分块 hash sam[太可怕了 不会写] JZPLCM    三维偏序,可持久化线段树维护 JZPEXT     数位统计(常数优化) JZPGYZ     suffix array水过 JZPCIR      找规律(OEIS水过) JZPFOR  三维哈密尔顿回路统计[太可怕了 不会] JZPLIT   构造 JZPLIT2  优化异或方程

末三位数

csdn上的一道编程练习题,我觉得称为(3 + √5)^n问题更贴切些 与其说这是一道编程题,不如说这是一道数论推导题. 好了,言归正装,下面开始题解 题意:让求(3 + √5)^n 的末三位数是多少,输出即可 首先令 an=(3 + √5)^n + (3 - √5)^n  这个an是一个整数,也就是一个整数可以用两个无理数表示,有人问了,为什么an是一个整数呢? 其实证明很简单,只需用(3 + √5) ^n和 (3 - √5)^n到高中所学的二次项分解即可,将(3 + √5)^n 和 (3 -