exclusive square
Enumerate six digits, but because he has a strong property, each digit is a different number, then you only need to dfs the full arrangement of 10 digits (only the first 6 digits are taken) and then square the result after It is enough to determine whether the digits have been used in turn.
#include <iostream>
using namespace std;
int st[10];
long long n;
void dfs(int k) {
if(k==6) {
if(n==203879) return;
long long cnt=n*n;
int flag=1;
while(cnt) {
if(st[cnt%10]) {
flag=0;
break;
}
cnt/=10;
}
if(flag) {
cout<<n<<endl;
exit(0);
}
return;
}
for(int i=0;i<10;i++) {
if(!k&&!i) continue;
if(!st[i]) {
st[i]=1; n=n*10+i;
dfs(k+1);
n/=10; st[i]=0;
}
}
}
int main()
{
dfs(0);
return 0;
}
Amount not available
Complete backpack. There are only two items in total, each item is unlimited, and only needs 01dp (0 means that the value cannot be obtained, 1 means that it can be obtained), and whether each item can be obtained depends on whether or not to take the item Can the number of
#include <iostream>
#include <vector>
using namespace std;
vector<int>res(2);
int q[100010];
int main()
{
for(int i=0;i<2;i++) cin>>res[i],q[res[i]]=1;
for(int i=0;i<2;i++) {
for(int j=res[i];j<100010;j++) {
q[j]|=q[j-res[i]];
}
}
for(int i=100009;i>=0;i--) {
if(!q[i]) {
cout<<i<<endl;
exit(0);
}
}
return 0;
}
palindrome date
Enumerate each date, determine whether the date is a legal date, and determine whether the date will ask. It will ask if the date is found and then find it if it is satisfied
ABABBABA
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int n;
int days[] = {
0,31,28,31,30,31,30,31,31,30,31,30,31 };
string rev(string ch) {
reverse(ch.begin(), ch.end());
return ch;
}
bool check(int k) {
string res = to_string(k);
return res == rev(res);
}
bool check_(int k) {
string res = to_string(k);
int a = res[0], b = res[1];
if(a==b) return false;
return res[2] == a && res[5] == a && res[7] == a && res[3] == b && res[4] == b && res[6] == b;
}
bool check_date(int k) {
int day = k % 100;
int month = (k % 10000) / 100;
int year = k / 10000;
if (month == 2) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) return day <= 28;
else return day <= 29;
}
if(month>12) return false;
return day <= days[month];
}
int main()
{
cin >> n;
int flag = 0;
for (int i = n + 1;; i++) {
if(!check_date(i)) continue;
if (!flag && check(i)) {
cout << i << endl;
flag = 1;
}
if (check_(i)) {
cout << i << endl;
break;
}
}
return 0;
}
joseph ring
The Joseph ring problem is a very classic problem, and the general approach is to find a pattern. But there are more blogs looking for regularities, so I will not write them (actually, I think I am not as good as them). Here is a simulation writing method that can not find regularity in a short time.
#include <iostream>
using namespace std;
int cal(int sum,int n,int num) {
if(num==1) return (sum+n-1)%sum;
return (cal(sum-1,n,num-1)+n)%sum;
}
int main()
{
int n,m;
cin>>n>>m;
cout<<cal(n,m,n)+1<<endl;
return 0;
}