周末训练笔记+UVA138+HDU2973【筛素数】【威尔逊定理】

来源:互联网 发布:java hello world 代码 编辑:程序博客网 时间:2024/05/27 20:58

这两天实在天冷了,比较适合宅起来敲俩代码,这俩个题目脑洞都比较大,之前没接触这个,先放上这俩,其余题目单发。

Street Numbers

 :有个程序员每天都遛狗,这很程序员,他家住的街道上是一条直线,一次往左依次往右,发现两次遛狗门牌号的和是一样的,问他家的门牌

思路:假设门牌是1-n,他家的是m,anemia根据等差数里求和公式,得出


直接暴力一发,tle,修改了输入处处还是超时,害羞直接复制粘贴超时输出的答案,反正就这么10组

YAPTCHA

 :

在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于实际操作意义不大。

题意:计算题目里的式子

思路:假设x=3k+7,原式子=((x-1)!+1)/x+(x-1)!/x

根据威尔逊定理则有(p-1)!+1==0  mod(p)    所以x为合数时为0,当x为素数时加1即可

where [x] denotes the largest integer not greater than x.

Street Numbers


 
A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it. 
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number): 
         6         8        35        49
Input
There is no input for this program.
Output
Output will consist of 10 lines each containing a pair of numbers, in increasing order with the last number, each printed right justified in a field of width 10 (as shown above).
Sample Input
Sample Output
         6         8        35        49
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long long#define N 1000000000int main(){    int num=0;    int n,k;    /*for(ll i=6;i<N;i++)    {        double temp=sqrt(0.5*(i*i+i));        int temp2=(int )temp;        if(temp==temp2&&num<10)        {            printf("%10d%10d\n",temp2,i);            num++;        }    }*/    cout<<"         6         8"<<endl;    cout<<"        35        49"<<endl;    cout<<"       204       288"<<endl;    cout<<"      1189      1681"<<endl;    cout<<"      6930      9800"<<endl;    cout<<"     40391     57121"<<endl;    cout<<"    235416    332928"<<endl;    cout<<"   1372105   1940449"<<endl;    cout<<"   7997214  11309768"<<endl;    cout<<"  46611179  65918161"<<endl;    return 0;}

YAPTCHA

 
The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle. 


However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program). 

The task that is presented to anyone visiting the start page of the math department is as follows: given a natural n, compute 

where [x] denotes the largest integer not greater than x.
Input
The first line contains the number of queries t (t <= 10^6). Each query consist of one natural number n (1 <= n <= 10^6).
Output
For each n given in the input output the value of Sn.
Sample Input
1312345678910100100010000
Sample Output
0112222334282071609
#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define ll long long#define N 5000010int vis[N];int prime[N];void Oula(int n){    int k=1;    for(int i=2;i<n;i++)    {        if(!vis[i])            prime[k++]=i;        for(int j=1;j<k&&i*prime[j]<n;j++)        {            vis[i*prime[j]]=1;            if(i%prime[j]==0)            break;        }    }}ll ans[1000010];int main(){    Oula(N);    ans[0]=ans[1]=0;    int temp=0;    for(int i=2;i<1000010;i++)    {        if(vis[3*i+7]==0)            temp=1;        else temp=0;        ans[i]=ans[i-1]+temp;    }    int n;    int t;    cin>>t;    while(t--)    {        cin>>n;        cout<<ans[n]<<endl;    }    return 0;}


原创粉丝点击