CF 84 div1 A

题目:109A - Lucky Sum of Digits

思路:扩展欧几里得

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
long long exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    else
    {
        long long ans=exgcd(b,a%b,x,y);
        long long t=x;
        x=y;
        y=t-a/b*y;
        return ans;
    }
}
int main()
{
    long long n;
    cin>>n;
    long long x,y;
    exgcd(4,7,x,y);
    x*=n;
    x%=7;
    if(x<0)
        x+=7;
    y=n-4*x;
    y/=7;
    if(x<0||y<0)
    {
        cout<<-1<<endl;
        return 0;
    }
    for(int i=1;i<=x;i++)
        cout<<4;
    for(int i=1;i<=y;i++)
        cout<<7;
    cout<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/overflow/p/3207989.html