ACM寒假集训数论专题
数论专题知识点主要包括:素数筛,最大公约数,拓展欧几里得定理,欧拉定理/费马小定理,中国剩余定理等.
部分题解如下:
Fedya and Maths
Fedya studies in a gymnasium. Fedya’s maths hometask is to calculate the following expression:
(1n + 2n + 3n + 4n) mod 5
for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).
Input
The single line contains a single integer n (0 ≤ n ≤ 1010e5). The number doesn’t contain any leading zeroes.
Output
Print the value of the expression without leading zeros.
大致题意
求(1n + 2n + 3n + 4n) mod 5.
思路
结果mod 5,所以只需要考虑1n + 2n + 3n + 4n的个位数.
而其中个位数有如下规律:
11=1,12=1,…
21=2,22=4,23=8,24=6,25=2,…
31=3,32=9,33=7,34=1,35=3,…
41=4,42=6,43=4,44=6,45=4,…
故1n + 2n + 3n + 4n的个位数mod 5按周期T=4循环.
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define finc(i,a,b) for(int i=(a);i<(b);i++)
#define fdec(i,a,b) for(int i=(b);i-->(a);)
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int test = 1; //cin >> test;
while (test--)
{
string str;
cin >> str;
int num, size = str.size();
if (str.size() >= 2)
num = ((str[size - 2] - '0') * 10 + str[size - 1] - '0') % 4;
else
num = (str[0] - '0') % 4;
int ans;
switch (num)
{
case 0:ans = 4; break;
default:ans = 0;
}
cout << ans << endl;
}
return 0;
}
Co-prime
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.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10e15) and (1 <=N <= 10e9).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
大致题意
统计区间[A,B]中所有与N的最大公因数等于1的数字的数目.
思路
只需求区间[1,B]及[1,A-1]中所有与N的最大公因数大于1的数字的数目,然后稍加运算即得结果.
而求区间[1,B]及[1,A-1]中所有与N的最大公因数大于1的数字的数目,可以先记录N的所有素因子,再运用容斥定理从[1,B]统计素因子中包括N的素因子的数字的数目即可.
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define finc(i,a,b) for(int i=(a);i<(b);i++)
#define fdec(i,a,b) for(int i=(b);i-->(a);)
ll f(ll a, vector<ll> & data)
{
ll ret = 0;
int size = data.size();
finc(i, 1, 1 << size)
{
ll x = 1, xx = 0;
finc(j, 0, size)
{
if ((1 << j) & i)
x *= data[j], t++;
}
ret += (xx & 1) ? a / x : -a / x;
}
return ret;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int test = 1; cin >> test;
int t = 0;
while (test--)
{
ll a, b, n; cin >> a >> b >> n;
if (n == 1)
{
cout << "Case #" << ++t << ": " << b - a + 1 << endl;
continue;
}
vector<ll> data;
if (n % 2 == 0)
{
data.push_back(2);
while (n % 2 == 0)
n >>= 1;
}
ll sqrtn = sqrt(n);
for (ll i = 3; i <= sqrtn; i += 2)
if (n % i == 0)
{
data.push_back(i);
while (n % i == 0)
n /= i;
}
if (n > 1)
data.push_back((int)n);
cout << "Case #" << ++t << ": " << b - f(b, data) - (a - 1 - f(a - 1, data)) << endl;
}
return 0;
}