Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
#include<bits/stdc++.h>
using namespace std;
int add(string &a,string &b,int *c)
{
int tmp,j=0; b=a;
for(int i=a.length()-1;i>=0;i--)
{
tmp=a[i]-'0';
c[j]=tmp;
j++;
}
j=0;
for(int k=b.length()-1;k>=0;k--)
{
tmp=b[k]-'0';
c[j]+=tmp;
if(c[j]>=10)
{
c[j]=c[j]%10;
c[j+1]+=1;
int m=j;
while(c[m+1]>=10)
{
c[m+2]+=1;
c[m+1]=c[m+1]%10;
m++;
}
}
j++;
}
if(c[j]==0)
return j-1;
else
return j;
}
int main()
{
string a,b; int flag=1,t; int c[21]={0};
int s[10]={0},g[10]={0};
getline(cin,a);
b=a;
for(int i=0;i<a.length();i++)
{
t=a[i]-'0';
s[t]++;
}
int j=add(a,b,c);
for(int i=0;i<=j;i++)
{
g[c[i]]++;
}
for(int i=0;i<10;i++)
{
if(s[i]!=g[i])
flag=0;
}
if(flag==1)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
for(int i=j;i>=0;i--)
cout<<c[i];
return 0;
}