您的位置:首页 > 编程语言 > C语言/C++

UVa 11440 Help Tomisu

2016-11-04 19:51 225 查看
After wasting a significant time of his life in problem-setting, Mr. Tomisu is now searching for glory: Aglory that will make him famous like Goldbach and rich like Bill Gates :). And he has chosen
the fieldof Number Theory as his prime interest. His creator did not make him very bright and so he needsyour help to solve an elementary problem, using which he will begin his pursuit for glory!Tomisu has come to know that finding out numbers having large
prime factors are very importantin cryptography. Given two integers N and M, he aims to count the number of integers X between 2and N! (factorial N), having the property that all prime factors of X are greater than M.

Input

The input file contains at most 500 lines of inputs. Each line contains two integers N (1 < N <10000001) and M (1 ≤ M ≤ N and N − M ≤ 100000). Input is terminated by a line containing twozeroes. This
line should not be processed.

Output

For each line of input produce one line of output. This line contains the value T %100000007 (Modulo100000007 value of T). Here T is the total number of numbers between 1 and N! (factorial N) whichhave
prime factors greater than M.

Sample Input

100 10

100 20

10000 9000

0 0

Sample Output

43274465

70342844

39714141

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

欧拉函数+神奇的思路~

这里的fi[i]表示的是小于i!的质因子个数,与一般的欧拉函数不同~

(好像欧拉函数应该是phi?)

这道题要求的是1…n!中有多少数与m!互质,也就相当于是求1…n中有多少数与m互质。

然后就能转化为求2..n!中有多少个x满足x的所有质因子都大于m,用欧拉函数可以很方便地求出来~

#include<cstdio>
#include<iostream>
using namespace std;
#define inf 10000000
#define modd 100000007
#define ll long long

ll n,m,a[inf+1],fi[inf+1];
bool b[inf+1];

void findd()
{
fi[1]=1;fi[2]=1;
for(ll i=2;i<=inf;i++)
{
if(!b[i]) a[++a[0]]=i;
for(ll j=1;a[j]*i<=inf;j++)
{
b[i*a[j]]=1;
if(!(i%a[j])) break;
}
}
for(int i=3;i<=inf;i++) fi[i]=(b[i] ? i*fi[i-1]:(i-1)*fi[i-1])%modd;
}

int main()
{
findd();
while(scanf("%lld%lld",&n,&m)==2 && n)
{
ll ans=fi[m];
for(ll i=m+1;i<=n;i++) ans=ans*i%modd;
printf("%lld\n",(ans-1+modd)%modd);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息