BNU 13259.Story of Tomisu Ghost 分解质因子

Story of Tomisu Ghost

It is now 2150 AD and problem-setters are having a horrified time as the ghost of a problem-setter from the past, Mr. Tomisu, is frequently disturbing them. As always is the case in most common ghost stories, Mr. Tomisu has an unfulfilled dream: he had set 999 problems throughout his whole life but never had the leisure to set the 1000th problem. Being a ghost he cannot set problems now so he randomly asks problem-setters to complete one of his unfinished problems. One problem-setter tried to convince him saying that he should not regret as 999 is nowhere near 1024 (210) and he should not worry about power of 10 being an IT ghost. But the ghost slapped him hard after hearing this. So at last one problem setter decides to complete his problem:

"n! (factorial n) has at least t trailing zeroes in b based number system. Given the value of n and t, what is the maximum possible value of b?"

Input

Input starts with an integer T (≤ 4000), denoting the number of test cases.

Each case contains two integers n (1 < n ≤ 105) and t (0 < t ≤ 1000). Both n and t will be given in decimal (base 10).

Output

For each case, print the case number and the maximum possible value of b. Since b can be very large, so print b modulo 10000019. If such a base cannot be found then print -1instead.

Sample Input

Sample Input

Output for Sample Input


4

1000 1000

1000 2

10 8

4 2


Case 1: -1

Case 2: 5227616

Case 3: 2

Case 4: 2

Source

题意:给你一个n,t,  n的阶乘在b进制下的数大小,数尾有t个0,问最大的b是多少,不存在b输出-1;

题解:数尾有t个0,也就是对于b有t倍的关系,我们将1-n内所有质因子的个数找出,是否有大于等于t的就可以加入答案,否则-1

///1085422276
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){
        if(ch==‘-‘)f=-1;ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘){
        x=x*10+ch-‘0‘;ch=getchar();
    }return x*f;
}
//****************************************
const double PI = 3.1415926535897932384626433832795;
const double EPS = 5e-9;
#define maxn 100000+5
#define mod 10000019

int n,t,H[maxn],HH[maxn];
vector<int >G[maxn],P;
ll pows(ll x,ll tt) {
    ll tmp=1;
   for(int i=1;i<=tt;i++) {
     tmp=(tmp*x)%mod;
   }
   return tmp;
}
int main() {
    int an=0;mem(HH);
    for(int i=2;i<=100000;i++) {
        if(!HH[i]) {P.pb(i);
        for(int j=i+i;j<=100000;j+=i) {
                    HH[j]=1;
        }
      }
    }
    int T=read(),oo=1;
    while(T--) {
        mem(H);
        int flag=0;
       n=read(),t=read();
       printf("Case %d: ",oo++);
       ll ans=1;
         for(int k=0;k<P.size()&&P[k]<=n;k++) {
             ll c=P[k],tt=0;
             ll y=n/z;
              while(y) {
                tt+=y;
                y=y/z;
              }
            if(tt>=t){
                    flag=1;
                ans=(ans*pows(c,tt/t))%10000019;
            }
         }
        if(!flag) printf("-1\n");
        else
            printf("%lld\n",ans);
    }
    return 0;
}

代码

时间: 2024-09-30 19:05:30

BNU 13259.Story of Tomisu Ghost 分解质因子的相关文章

分解质因子(个人模版)

分解质因子: 1 memset(prime,0,sizeof(prime)); 2 memset(num,0,sizeof(num)); 3 for(int i=2;i<=5000005;i++) 4 { 5 if(prime[i]==0) 6 { 7 for(int j=i;j<=5000005;j+=i) 8 { 9 int temp=j; 10 while(temp%i==0) 11 { 12 num[j]++; 13 temp/=i; 14 } 15 prime[i]=1; 16 }

UVA10892 - LCM Cardinality(分解质因子)

题目链接 题意:输入正整数n,统计有多少对正整数a <= b,满足lcm(a, b) = n. 思路:分解质因子,然后直接暴力求出对数 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; const int MA

HDU 4497 GCD and LCM(分解质因子+排列组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

POJ 1730 Perfect Pth Powers (枚举||分解质因子)

Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16638   Accepted: 3771 Description We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More g

poj3993Not So Flat After All(筛法素数+分解质因子)

题目链接: 啊哈哈,点我点我 题意: 题意是给出两个数字,然后有由一分解定理得,每个数可以分解成若干质因数的乘积,这样就可以在一个n维的坐标系下表示出这个点...比如给出50和24 因为24=2^3*3^1*5^0  而50=2^1*3^0*5^2那么这两个点就可以在一个3维德坐标系下表示出这两个点..24=(3,1,0)  50=(1,0,2)  那么共同拥有的维度就是3  而两个点在n维坐标系下的距离就是|3-1|+|1-0|+|0-2|=5,这样题意完全理解... 思路: 先筛出10000

HDU 3641 Treasure Hunting (二分+分解质因子)

题目链接:HDU 3641 Treasure Hunting 题意:求X!%M==0中最小的的X.其中M=a1^b1*a2^b2*a3^b3.... 思路:求余为0想到整除,即分母的因子构成的集合是分子的因子构成的集合的子集.因子又想到,任何一个整数都可以分解成若干个素数相乘. 注意:题目数据很大,查到答案时二分. AC代码: #include<stdio.h> #include<string.h> #define ll __int64 bool Prime[210]; ll nu

分解质因子问题

题目大意:有t(1<=t<=104)个数arr[1],arr[2]-.arr[t],设每个数是n(2<=n<=109),任务是将这个n的质因子分解出来,包括重复的质因子,时限是1000MS..比如n=18,而18=2*3*3,所以输出的结果就是2 3 3. n的范围是[2,109],很容易想到n的质因子的范围是[2,sqrt(n)],所以可以预处理出[2,sqrt(109)]的所有素数(注:sqrt()就是求解一个数的平方根),这个范围内的素数大概只有3千多个,拿这个n跟所有的素数

UVA10780 - Again Prime? No Time.(分解质因子)

题目链接 题意:输入两个整数n和m,求最大的整数k使得m^k是n!的约数. 思路:m^k等于m的所有质因子的k次方的和,所以只要找到m中的质因子在n!中所能得到的最小的次方,就是k的值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int I

HDU 4135 Co-prime (容斥+分解质因子)

<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. 解题分析: 将求区间[A,B]与N互质的数转化成求[1,B] 区间与N互质的个数  -  [1,A-1]中与N互质的个数.同时,因为直接求区间内与N互质的数不好求,我们从反面入手,求出与N不互质的数,借鉴埃筛的思想,我们先求出N的所有质因子,然后将这些质因子在区间内倍数的个数全部求出(即与N不互质的数),再用