杂题

A - Twin Prime Conjecture

HDU - 3792                    

If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.InputYour program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.OutputFor each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.Sample Input

1
5
20
-2

Sample Output

0
1
4
#include"stdio.h"
#include<map>
int a[100000]={0};
using namespace std;
int main()
{
     extern int a[100000];
    int i,j,n;
    scanf("%d",&n);
    map<int,int>count;count[0]=1;count[1]=1;a[5]=1;
    for(i=2;i<100000;i++)
    {
        if(count[i]==0)
        {
            for(j=2;i*j<=100000;j++)
            {
                count[i*j]=1;    
            }
        }
    }
    for(i=1;i<=100000;i++)
    {
        a[i+2]=a[i+1];
        if(count[i]==0&&count[i+2]==0)
        a[i+2]=a[i+1]+1;
    }
    while(n>=0)
    {
        printf("%d\n",a[n]);
        scanf("%d",&n);
    }
} 

B - 美素数

HDU - 4548                    

小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识。
  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。
  给定一个区间,你能计算出这个区间内有多少个美素数吗?Input第一行输入一个正整数T,表示总共有T组数据(T <= 10000)。
接下来共T行,每行输入两个整数L,R(1<= L <= R <= 1000000),表示区间的左值和右值。Output对于每组数据,先输出Case数,然后输出区间内美素数的个数(包括端点值L,R)。
每组数据占一行,具体输出格式参见样例。Sample Input

3
1 100
2 2
3 19

Sample Output

Case #1: 14
Case #2: 1
Case #3: 4

C - 又见GCD

HDU - 2504                     
有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
Input第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。
Output输出对应的c,每组测试数据占一行。
Sample Input
2
6 2
12 4
Sample Output
4
8
#include"stdio.h"
int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int main()
{
    int n,i,j,a[100000],b[100000],c;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    for(i=0;i<n;i++)
    {
        for(c=b[i]+1;;c++)
        {
            if(gcd(a[i],c)==b[i])
            {
                printf("%d\n",c);break;
            }
        }
    }
}

D - 青蛙的约会

POJ - 1061                    

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input

1 2 3 4 5

Sample Output

4
#include"stdio.h"
long long int gcd(long long int a,long long int b)
{
    if(b==0)
    return a;
    return gcd(b,a%b);
}
long long int extgcd(long long int a,long long int b,long long int &X,long long int &Y)
{
    long long int d=a;
    if(b!=0)
    {
        d=extgcd(b,a%b,Y,X);
        Y=Y-a/b*X;
    }
    else
    {
        X=1;Y=0;
    }
    return d;
}

int main()
{
    long long int t,a,b,r,x,y,m,n,d,l,X=0,Y=0;
    while(scanf("%lld %lld %lld %lld %lld",&x,&y,&m,&n,&l)!=EOF)
    {
        a=n-m;b=l;d=gcd(a,b);
        if((x-y)%d==0)
        {
            a/=d;b/=d;
            extgcd(a,b,X,Y);
            t=(X*((x-y)/d))%b;
            while(t<0)
            {
                t+=b;
            }
            printf("%lld\n",t);
        }
        else
        printf("Impossible\n");
    }    
} 

E - A/B

HDU - 1576                    

要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。Input数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。Output对应每组数据输出(A/B)%9973。Sample Input

2
1000 53
87 123456789

Sample Output

7922
6060
#include"stdio.h" 
long long int extgcd(long long int a,long long int b,long long int &x,long long int &y)
{
    long long int d;
    if(b!=0)
    {
        d=extgcd(b,a%b,y,x);
        y=y-a/b*x;
    }
    else
    {
        x=1;y=0;d=a;
    }
    return d;
}
int main()
{
    long long int d,a,b,n,x=0,y=0,t,i;
    scanf("%lld",&t);
    for(i=0;i<t;i++)
    {
        a=9973;
        scanf("%lld%lld",&n,&b);
        d=extgcd(a,b,x,y);y=y*(n/d);
        printf("%lld\n",(y%9973+9973)%9973);
    }
}

F - Tr A

HDU - 1575                    

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是0,90,9 ,表示方阵A的内容。
Output对应每组数据,输出Tr(A^k)%9973。Sample Input

2
2 2
1 0
0 1
3 99999999
1 2 3
4 5 6
7 8 9

Sample Output

2
2686

G - 人见人爱A^B

HDU - 2035 
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
Input输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。Output对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Sample Input
2 3
12 6
6789 10000
0 0
Sample Output
8
984
1
#include"stdio.h"
int main()
{
    long long int i,j,a,b;
    scanf("%lld%lld",&a,&b);
    while(1)
    {
        if(a==0&&b==0)
        break;j=1;
        for(i=0;i<b;i++)
        {
            if(j>1000)
            j=j*a-(j*a)/1000*1000;
            else
            j=j*a;
        }
        printf("%lld\n",j-j/1000*1000);
        scanf("%lld%lld",&a,&b);
    }    
}

H - A hard puzzle

HDU - 1097                    

lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
InputThere are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
OutputFor each test case, you should output the a^b's last digit number.
Sample Input

7 66
8 800

Sample Output

9
6
#include"stdio.h"
typedef long long ll;
long long int mod_pow(long long int a,long long int b)
{
    ll res=1;
    while(b>0)
    {
        if(b&1) res=res*a%1000;
        a=a*a%1000;
        b>>=1;
    }
    return res;
}
int main()
{
    ll i,a,b;
    while(scanf("%lld%I64d",&a,&b)!=EOF)
    {
        i=mod_pow(a,b);
        printf("%lld\n",(i%10+10)%10); 
    }
} 

 



转载于:https://www.cnblogs.com/lch316/p/6771179.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值