1. 程式人生 > >結論 (自己未曾學到的知識補充)

結論 (自己未曾學到的知識補充)

-- 相親 .org IT TP ID 求和 scanf i+1

以下幾乎全部抄的題解(方便自己復習)
因為這些我見都沒見過(_(:з」∠)_蒟蒻瑟瑟發抖

LuoGu P1887 乘積最大3

請你找出M個和為N的正整數,他們的乘積要盡可能的大。 輸出字典序最小的一種方案。

扔代碼

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n,m;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main ()
{
    read(n);read(m);
    
    int tot = n / m;
    for (int i=1;i<=m;i++)
    {
        if(i<m-(n-tot*m)+1)
        {
            printf("%d ",tot);
        } else printf("%d ",tot+1);
    } return 0;
}

LuoGu P1075 質因數分解

一個數有且只能分解為一組質數的乘積

扔代碼

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int k;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    read(k);
    for (int i=2;i<=k;i++)
    {
        if(k%i == 0)
        {
            printf("%d\n",k/i);
            return 0;
        }
    } return 0;
}

LuoGu P3717 [AHOI2017初中組]cover

圓覆蓋問題..大概就是公式求圓覆蓋(兩點間的距離公式然後直到半徑好像也能求)

扔代碼

//#define fre yes

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 2005;
int Map[maxn][maxn];

int n,m,r,ans;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = - 1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

void kis(int x,int y,int z)
{
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if(pow(i-x,2)+pow(j-y,2) <= pow(r,2))
            {
                Map[i][j] = 1;
            } }
    }
}

int main ()
{
    read(n);read(m);read(r);
    for (int i=1;i<=m;i++)
    {
        int x,y;
        read(x);read(y); 
        Map[x][y] = 1;
        kis(x,y,r);
    } 
    for(int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if(Map[i][j] == 1)
            {
                ans++;
            }
        }
    } printf("%d\n",ans);
    return 0;
}

LuoGu P2415 集合求和

給定一個集合s(集合元素數量<=30),求出此集合所有子集元素之和

樣例
輸入 2 3 輸出 10
解釋:
[] [2] [3] [2 3] 2+3+2+3=10

扔代碼

//#define fre yes

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 35;
int arr[maxn];

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main ()
{
    int k = 1;
    while(cin >> arr[k]) { k++; }
    
    long long s = 0;
    for (int i=1;i<=k;i++)
    {
        s += arr[i];
    } s *= pow(2, k-2);
    printf("%lld\n",s);
    return 0;
}

LuoGu P1851 好朋友

相親數:兩個數中任意一個的所有因子(除本身外)之和等於另外一個數

扔代碼

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 100005;
int pr[maxn];

int S;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

void cur()
{
    // O(n^2)
    // for(int i=1;i<=100000;i++)
    // {
    //     for(int j=1;j<i;j++)
    //     {
    //         if(i%j==0)
    //         {
    //             pr[i]+=j;
    //         }
    //     }
    // }

    //O(n sqrt(n))
    int j;
    for (int i=1;i<=100000;i++)
    {
        for (j=1;j*j<=i;j++)
        {
            if(i%j==0)
            {
                pr[i] += j;
                pr[i] += i/j;
            }
        } j--;
        if(j*j == i) pr[i] -= j;
        pr[i] -= i;
    }

    //O(n log(log(n)))
    // for(int i=1;i<=20000;i++)
    //     for(int j=i;i*j<=20000;j++)
    //         pr[i*j]+=i;
}

int main()
{
    read(S);
    cur();
    for (int i=S;;i++)
    {
        if(pr[pr[i]] == i&&pr[i]!=i)
        {
            printf("%d %d",i,pr[i]);
            return 0;
        }
    } return 0;
}

LuoGu https://www.luogu.org/problemnew/show/P1548

有一個棋盤 給你n m 找有多少個正方形和長方形

扔代碼

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int ans1,ans2;
int n,m;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main ()
{
    read(n);read(m);
    ans2 = ((m+1)*(n+1)*m*n)/4;
    for(int i=n,j=m;j>=1&&i>=1;j--,i--) {
        ans1 += i*j;
    } printf("%d %d\n",ans1,ans2-ans1);
    return 0;
}

LuoGu P2181 對角線

凸多邊形找對角線交點個數

扔代碼

//#define fre yes

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

unsigned long long n;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    read(n);
    n = (n*(n-1)/2*(n-2)/3*(n-3)/4);
    printf("%lld\n",n);
    return 0;
}

LuoGu P1469 找筷子

利用 a^a=0

//#define fre yes

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n,ans;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1 ; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    read(n);
    for (int i=1;i<=n;i++)
    {
        int x;
        read(x);
        ans = (ans ^ x);
    } printf("%d\n",ans);
    return 0;
}

LuoGu P1100 高低位交換

C++的32位無符號整形(話說有這個嗎 Orz)
左移16位,就是低位轉到高位;右移16位,就是高位轉到低位;兩者相加,就是新數

扔代碼

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    scanf("%u", &n);
    printf("%u\n", (n >> 16) + (n << 16));
    return 0;
}

LuoGu P1147 連續自然數和

設首項為L,末項為R,那麽sum(L,R)=(L+R)(R-L+1)/2=M
即(L+R)(R-L+1)=2M
可以把2M分解成兩個數之積,假設分成了兩個數K1,K2,且K1<K2時,
可以列一個二元一次方程組
R-L+1=K1
L+R=K2 解得L=(K2-K1+1)/2, R=(K1+K2-1)/2
當K1,K2一奇一偶時,L,R才有自然數解.
不過有一種特殊情況,就是L=R的情況,這種情況是不允許的
即(K2-K1+1)/2≠(K1+K2-1)/2,解得K1≠1

扔代碼

//#define fre yes

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main(){
    read(n);
    for(int i=sqrt(2*n);i>1;i--)
    {
        if(2*n%i==0 && (i+2*n/i)%2)
        {
            int j=2*n/i;
            printf("%d %d\n",(j-i+1)/2,(i+j-1)/2);
        }
    } return 0;
}

LuoGu P1497 木牛流馬

讀了半天題都沒讀懂..但是題解寫的的確是結論

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n,k,h;

long long ans = 1;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    read(n);read(k);read(h);
    if(k > n)
    {
        puts("0");
        return 0;
    }
    for (int i=1;i<=h;i++)
    {
        int x;
        read(x);
        for(int j=1;j<=x;j++)
         ans *= j;
    } long long ans_g = 1;
    for (int i=n;i>=n-k+1;i--) ans_g *= i * i;
    printf("%lld\n",ans_g/ans);
    return 0;
}

LuoGu P2807 三角形計數

把大三角形的每條邊n等分,將對應的等分點連接起來

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

long long ans,n;
int k;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == ‘-‘) lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - ‘0‘;c = getchar(); } while(isdigit(c));
    x *= lenp;
}

int main()
{
    read(k);
    while(k--)
    {
        ans = 0;
        read(n);
        for (long long i=0;n-i!=0;i++)
            ans += (n-i) * (n-i+1);
        for (long long i=0;n-i>=2;i+=2)
            ans += (n-i) * (n-i-1);
        ans /= 2;
        printf("%lld\n",ans);
    } return 0;
}

結論 (自己未曾學到的知識補充)