Just Say hello to algorithm

Source: Internet
Author: User

-I am so short of practice and so stupid.

Just Say hello to algorithm

Do a little before the simple topic, a is very water, however, WA once, because no greater than 2^32 to open long long, with int not, int range is -2^32~2^32-1

Question F

Design Tutorial:learn from Math

Test instructions: give you a number greater than or equal to 12, you need to use two composite to express it. Composite refers to the number of natural numbers that can be divisible (excluding 0) by other numbers except those that are 1 and the whole.

Analysis: Why give you big and equal to 12, we know even 2 is composite, so give you an even, you subtract an even number to get is even, because n>=12, so minus 4, get is >=8 of even, certainly is composite, of course you can also subtract 6, 8, more than 8 will not be.

And give you an odd number? You subtract an odd number and get an even number, minus one is an odd number of composite, the smallest is 9, odd: n>=13,n-9>=4

#include <stdio.h>int main () {    int A, b;    int n;    scanf ("%d", &n);    if (n%2) {        printf ("%d%d\n", 9,n-9);    } else{        printf ("%d%d\n", 8,n-8);    }    return 0;}

This problem can also be enumerated

#include <stdio.h>int iscomposite (int a) {    if (a==2) return 0;    for (int i=2;i*i<=a;i++)        if (a%i==0) return 1;    return 0;} int main () {    int a,n;    scanf ("%d", &n);    for (a=2;a<n;a++)        if (Iscomposite (a) &&iscomposite (n-a)) {            printf ("%d%d", a,n-a);            break;        }    return 0;}

  

H Question pseudoprime numbers

Test instructions: Ask 1^m + 2^m + 3^m + ... + n^m (answer mod 1e9+7)

Note: Each time you add up and add up the MoD

#include <stdio.h> #define M 1000000007long long Qpow (Long long A,long long M)//fast power {    long long ans=1,k=a;    while (m)    {        if (m&1)            ans= (ans*k)%M;        k= (k*k)%M;        m>>=1;    }    return ans;} int main () {    long long N,m,ans;    while (scanf ("%lld%lld", &n,&m)!=eof)    {        ans=0;        for (int i=1; i<=n; i++)            Ans+=qpow (i,m)%m;//here mod        a bit printf ("%lld\n", ans%m);//mod here    }    return 0;}

  

G-Question Swapsort

Test instructions: Using less than n times of swap to arrange the order.

Analysis: Select sort, choose the smallest one after unsorted and then the first interchange that is not sorted, so that a maximum of n-1 can be sorted and the two-digit position of the interchange is recorded in an array.

#include <stdio.h>int n,i,j,a[3002],coun,temp,swap1[3001],swap2[3001];int Main () {    scanf ("%d", &n);    for (i=0; i<n; i++)        scanf ("%d", a+i);    for (i=0; i<n-1; i++)    {        int minj=i;        for (j=i+1; j<n; j + +)            if (A[j]<a[minj]) minj=j;        if (I!=minj)        {            swap1[coun]=i;            Swap2[coun]=minj;            Temp=a[minj];            A[minj]=a[i];            A[i]=temp;            coun++;        }    }    printf ("%d\n", Coun);    if (Coun)        for (i=0; i<coun; i++)            printf ("%d%d\n", Swap1[i],swap2[i]);    return 0;}

  

-I'm too stupid to refuel!

Just Say hello to algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.