bc19&&hdoj5108&&hdoj5109

Alexandra and Prime Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1632    Accepted Submission(s): 557


Problem Description
Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime.
The problem above is quite easy, so Alexandra gave him a new task: Given a positive integer N, find the minimal positive integer M, such that N/M is prime. If such M doesn't exist, output 0.
Help him!
 

Input
There are multiple test cases (no more than 1,000). Each case contains only one positive integer N.
N1,000,000,000 .
Number of cases with  N>1,000,000  is no more than 100.
 

Output
For each case, output the requested M, or output 0 if no solution exists.
 

Sample Input
  
  
3 4 5 6
 

Sample Output
  
  
1 2 1 2
 

Source
 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
bool prime(long long n){
    if(n==1)return false;
    for(long long i=2;i*i<=n;++i){
        if(n%i==0)return false;
    }
    return true;
}
int main()
{
    long long n,i;
    while(scanf("%lld",&n)!=EOF){
        long long ans=inf;
        bool sign=false;
        for(i=1;i*i<=n;++i){
            if(n%i==0){
                if(prime(i)){
                    sign=true;
                    ans=min(ans,n/i);
                }
                if(prime(n/i)){
                    sign=true;
                    ans=min(ans,i);
                }
            }
        }
        if(sign)
            printf("%lld\n",ans);
        else 
            printf("0\n");
    }
    return 0;
}

Alexandra and A*B Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 777    Accepted Submission(s): 199


Problem Description
Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given two positive integers A and B, output A*B.
This problem is even easier than the last one. Alexandra can't wait to give him another task: Given a positive integer A and a string S(S only contains numbers.), find the minimum positive integer B, such that S is a substring of T, where T is the decimal notation of A*B.
See the sample for better understanding.
Note: S can contain leading zeros, but T can't.
 

Input
There are multiple test cases (no more than 500). Each case contains a positive integer A and a string S.
A10,000,1|S|8 .
 

Output
For each case, output the required B. It is guaranteed that such B always exists.
To C++ programmers: if you want to output 64-bit integers, please use "%I64d" specifier or cout.
 

Sample Input
   
   
6 8 96 19 2 0086 1 1
 

Sample Output
   
   
3 2 5043 1
 

Source
 

最后的A*B应该是xSy的形式,其中y可以为空,并且如果S不以0开头那么x也可以为空。 首先枚举y的长度。在y的长度确定之后,显然x应该越小越好。所以从小到大枚举x。设S的长度为p,y的长度为q,那么可以列方程: ((x∗10p+S)∗10q+y)modA=0((x*10^p + S)*10^q + y) mod A = 0((x10p+S)10q+y)modA=0 。解出y以后,如果 y<10qy<10^qy<10q 那么解合法。 容易证明,我们只需从0(或1,如果S以0开头)到A枚举x,那么所有的解一定会被枚举到。对于q=0的情况,x的长度不会超过4(因为A<=10000),所以y的长度也只需要枚举到4就可以了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define inf 0x7ffffff
using namespace std;
char bit[10];
long long Pow[20];
void init(){
    Pow[0]=1;
    for(int i=1;i<=18;++i){
        Pow[i]=Pow[i-1]*10;
    }
}
int main()
{
    init();
    int i,j,k;
    long long n,ans,pre;
    while(scanf("%lld%s",&n,bit)!=EOF){
        int len=strlen(bit);ans=1e18;
        long long num;sscanf(bit,"%lld",&num);
        for(i=0;i<5;++i){//枚举y的长度
            for(pre=0;pre<=Pow[4-i];++pre){//枚举x
                if(bit[0]=='0'&&pre==0)continue;
                if(bit[0]!='0'&&pre==Pow[4-i])continue;
                long long cnt=pre*Pow[len+i]+num*Pow[i];
                long long l=(n-cnt%n)%n;
                if(l<Pow[i])ans=min(ans,cnt+l);
            }
        }
        printf("%lld\n",ans/n);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值