CodeForces - 376C~Divisible by Seven(思路)

C. Divisible by Seven
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.

Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.

Input

The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 106 characters.

Output

Print a number in the decimal notation without leading zeroes — the result of the permutation.

If it is impossible to rearrange the digits of the number a in the required manner, print 0.

Examples
input
1689
output
1869
input
18906
output

18690

题意是给一串数字,其中一定含有1 6 8 9,其他的随意,将这一串数字随意排序,输出可以整除7的
同余定理
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<algorithm>
#include<vector>
#define inf 1000000007
#define LL long long int
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char a[1000005];
int vis[10];
int main()
{
    mem(vis,0);
    int mod=0,sum=0;
    scanf("%s",a);
    int n=strlen(a);
    for(int i=0;i<n;i++)
    {
        //判断第一次的1,6,8,9,第二次遇到1,6,8,9直接输出,然后判断余数
        if(a[i]=='1'&&!vis[1])
        {
            vis[1]=1;
            continue;
        }
        else if(a[i]=='6'&&!vis[6])
        {
            vis[6]=1;
            continue;
        }
        else if(a[i]=='8'&&!vis[8])
        {
            vis[8]=1;
            continue;
        }
        else if(a[i]=='9'&&!vis[9])
        {
            vis[9]=1;
            continue;
        }
        else if(a[i]=='0')
        sum++;
        else
            {
                mod=(mod*10+(a[i]-'0'))%7;
                printf("%c",a[i]);
            }
    }
    //由于1,6,8,9占了四位,所以要乘10000
    mod=(mod*10000)%7;
    //计算1689的各种组合,从0~6余数,输出
    if(mod==0)
        printf("1869");
    else if(mod+1==7)
        printf("1968");
    else if(mod+2==7)
        printf("1689");
    else if(mod+3==7)
        printf("6198");
    else if(mod+4==7)
        printf("1698");
    else if(mod+5==7)
        printf("1986");
    else if(mod+6==7)
        printf("1896");
        for(int i=0;i<sum;i++)
            printf("0");

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值