大菜鸡的数论之旅-[kuangbin]数学训练四 数论
A题 LightOJ 1007 Mathematically Hard
Problem Description
Mathematically some problems look hard. But with the help of the computer, some problems can be easily solvable.
In this problem, you will be given two integers a and b. You have to find the summation of the scores of the numbers from a to b (inclusive). The score of a number is defined as the following function.
score (x) = n2, where n is the number of relatively prime numbers with x, which are smaller than x
For example,
For 6, the relatively prime numbers with 6 are 1 and 5. So, score (6) = 22 = 4.
For 8, the relatively prime numbers with 8 are 1, 3, 5 and 7. So, score (8) = 42 = 16.
Now you have to solve this task.
Input
Input starts with an integer T (≤ 105), denoting the number of test cases.
Each case will contain two integers a and b (2 ≤ a ≤ b ≤ 5 * 106).
Output
For each case, print the case number and the summation of all the scores from a to b.
Input
3
6 6
8 8
2 20
Output
Case 1: 4
Case 2: 16
Case 3: 1237
Note
Euler’s totient function applied to a positive integer n is defined to be the number of positive integers less than or equal to n that are relatively prime to n. is read “phi of n.”
Given the general prime factorization of , one can compute using the formula
---------------------------------------分割线------------------------------------------------------------------------------------
题意
求a,b内所有数的欧拉函数的平方和
思路
由于T比较大,且a,b范围较大,所以考虑用预处理的方式O(1)查询
坑点
直接用LL存前缀和会炸LL,所以必须用unsigned LL,而且欧拉函数平方的时候也要转为ULL。
--------------------------------分割线-------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
#define __bug(x) cout<<"test:"<<x<<endl;
using namespace std;
typedef unsigned long long ll;
const int N=5e6+5;
int res[N];
ll pre[N];
void euler()
{
memset(res,0,sizeof(res));
res[1]=1;
pre[0]=0;
for(int i=2; i<N; ++i) //先打表出每个数的互质数个数
{
if(!res[i])
{
for(int j=i; j<N; j+=i)
{
if(!res[j])
res[j]=j;
res[j]=res[j]/i*(i-1); //欧拉函数
}
}
}
for(int i=1;i<N;i++)
{
pre[i]=pre[i-1]+(ll)res[i]*res[i];
// cout<<i<<' '<<pre[i]<<endl;
}
}
int main()
{
euler();
int T;
cin>>T;
// __bug(res[1]);
for(int cas=1; cas<=T; cas++)
{
int a,b;
cin>>a>>b;
printf("Case %d: %llu\n",cas,pre[b]-pre[a-1]);
}
return 0;
}
-----------------------------------------分割线----------------------------------------------------------------------------------
B题 LightOJ 1014 Ifter Party
Problem Description
I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited C contestants and arranged P piaju’s (some kind of food, specially made for Ifter). Each contestant ate Q piaju’s and L piaju’s were left (L < Q).
Now you have to find the number of piaju’s each contestant ate.
Input
Input starts with an integer T (≤ 325), denoting the number of test cases.
Each case contains two non-negative integers P and L (0 ≤ L < P < 231).
Output
For each case, print the case number and the number of possible integers in ascending order. If no such integer is found print ‘impossible’.
Input
4
10 0
13 2
300 98
1000 997
Output
Case 1: 1 2 5 10
Case 2: 11
Case 3: 101 202
Case 4: impossible
-----------------------------------------------------分割线----------------------------------------------------------------------
题意
有p克蛋糕,每人吃q克,剩余l克,问q的值有多少种情况,并从小到大输出。
思路
即求p-l的所有大于l的因数
坑点
无(我要是再因为输出里少了空格而WA,我直播吃了电脑)。
---------------------------------------------------分割线------------------------------------------------------------------------
#include <bits/stdc++.h>
#define __bug(i,x) cout<<"test "<<i<<" :"<<x<<endl;
using namespace std;
typedef long long ll;
const int N=1e6+5;
ll ans[N];
bool cmp(ll a,ll b)
{
return a<b;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
int cnt=0;
ll l,p;
cin>>p>>l;
for(ll i=1;i<=(int)sqrt(p-l);i++)
{
if((p-l)%i==0)
{
if(i>l&&i<=p-l)
ans[cnt++]=i;
if((p-l)/i>l&&(p-l)/i<=p-l&&(p-l)/i!=i)
ans[cnt++]=(p-l)/i;
}
}
sort(ans,ans+cnt,cmp);
printf("Case %d:",cas);
for(int i=0;i<cnt;i++)
{
cout<<" "<<ans[i];
}
if(cnt==0)
cout<<" impossible";
cout<<endl;
}
return 0;
}
-----------------------------------------------分割线----------------------------------------------------------------------------
C题 LightOJ 1024 Eid
Problem Description
In a strange planet there are n races. They are completely different as well as their food habits. Each race has a food-eating period. That means the ith race eats after every xi de-sec (de-sec is the unit they use for counting time and it is used for both singular and plural). And at that particular de-sec they pass the whole day eating.
The planet declared the de-sec as ‘Eid’ in which all the races eat together.
Now given the eating period for every race you have to find the number of de-sec between two consecutive Eids.
Input
Input starts with an integer T (≤ 225), denoting the number of test cases.
Each case of input will contain an integer n (2 ≤ n ≤ 1000) in a single line. The next line will contain n integers separated by spaces. The ith integer of this line will denote the eating period for the ith race. These integers will be between 1 and 10000.
Output
For each case of input you should print a line containing the case number and the number of de-sec between two consecutive Eids. Check the sample input and output for more details. The result can be big. So, use big integer calculations.
Sample Input
2
3
2 20 10
4
5 6 30 60
Sample Output
Case 1: 20
Case 2: 60
-------------------------------------------分割线--------------------------------------------------------------------------------
题意
求n个数的最小公倍数
思路
将n个数分解质因数,记录对于每个质因数的最大值,累乘即可。
坑点
因为最小公倍数LL存不下,所以本题的实质是高精度乘单精度。
-------------------------------------------分割线--------------------------------------------------------------------------------
代码
#include <bits/stdc++.h>
using namespace std;
const double PI=acos(-1.0);
const int N=1e5+5;
int ans[N],len;
void multiply(int fact)
{
for(int i=0; i<len; i++)
ans[i]*=fact;
for(int i=0; i<len; i++)
{
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
int tmp=ans[len];
while(tmp)
{
ans[len++]=tmp%10;
tmp/=10;
}
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
map<int,int> M;
M.clear();
memset(ans,0,sizeof(ans));
ans[0]=1;
len=1;
int n;
scanf("%d",&n);
while(n--)
{
int x;
scanf("%d",&x);
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
int cnt=0;
while(x%i==0)
{
cnt++;
x/=i;
}
M[i]=max(M[i],cnt);
}
}
if(x>=0)
{
M[x]=max(M[x],1);
}
}
map<int,int> ::iterator it;
for(it=M.begin();it!=M.end();it++)
{
// cout<<"test:"<<(it->first)<<' '<<(it->second)<<endl;
for(int i=0;i<(it->second);i++)
{
multiply((it->first));
}
}
printf("Case %d: ",cas);
for(int i=len-1;i>=0;i--)
printf("%d",ans[i]);
printf("\n");
}
return 0;
}
----------------------------------------------分割线-----------------------------------------------------------------------------